*,
*::before,
*::after {
  box-sizing: border-box;
}

:root,
[data-theme="dark"] {
  --bg: #0f1117;
  --surface: #161b22;
  --border: #30363d;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --accent: #58a6ff;
  --accent-hover: #79b8ff;
  --accent-subtle: color-mix(in srgb, var(--accent) 12%, transparent);
  --btn-primary-text: #0d1117;
  --drop-bg: #1c2128;
  --preview-bg: #0d1117;
  --code-bg: #161b22;
  --danger: #f85149;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
  --radius: 8px;
  --header-h: 56px;
  --hl-keyword: #ff7b72;
  --hl-type: #79c0ff;
  --hl-string: #a5d6ff;
  --hl-number: #ffa657;
  --hl-comment: #8b949e;
  --hl-punctuation: #c9d1d9;
}

[data-theme="light"] {
  --bg: #ffffff;
  --surface: #f6f8fa;
  --border: #d0d7de;
  --text: #1f2328;
  --text-muted: #656d76;
  --accent: #0969da;
  --accent-hover: #0550ae;
  --accent-subtle: color-mix(in srgb, var(--accent) 10%, transparent);
  --btn-primary-text: #ffffff;
  --drop-bg: #f6f8fa;
  --preview-bg: #ffffff;
  --code-bg: #f6f8fa;
  --danger: #cf222e;
  --hl-keyword: #cf222e;
  --hl-type: #0550ae;
  --hl-string: #0a3069;
  --hl-number: #953800;
  --hl-comment: #656d76;
  --hl-punctuation: #24292f;
}

html {
  color-scheme: light dark;
}

[data-theme="dark"] {
  color-scheme: dark;
}

[data-theme="light"] {
  color-scheme: light;
}

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

.hidden,
[hidden] {
  display: none !important;
}

/* Header */

.header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  height: var(--header-h);
  padding: 0 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header__brand {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

.header__logo {
  width: 22px;
  height: 22px;
  display: block;
  object-fit: contain;
}

.header__title {
  font-weight: 600;
  font-size: 1rem;
  letter-spacing: -0.01em;
}

.header__actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.header__filename {
  font-size: 0.8125rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 280px;
}

/* Buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.4375rem 0.875rem;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  white-space: nowrap;
}

.btn--icon {
  padding: 0.4375rem;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn--icon:hover {
  color: var(--text);
  background: var(--drop-bg);
}

.btn--primary {
  background: var(--accent);
  color: var(--btn-primary-text);
}

.btn--primary:hover {
  background: var(--accent-hover);
}

.btn--ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn--ghost:hover {
  color: var(--text);
  background: var(--drop-bg);
}

.icon {
  width: 18px;
  height: 18px;
  display: block;
}

[data-theme="dark"] .icon--sun { display: block; }
[data-theme="dark"] .icon--moon { display: none; }
[data-theme="light"] .icon--sun { display: none; }
[data-theme="light"] .icon--moon { display: block; }

/* Main */

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* Dropzone */

.dropzone {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  margin: 1.5rem;
  border: 2px dashed var(--border);
  border-radius: calc(var(--radius) * 1.5);
  background: var(--drop-bg);
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.dropzone:hover,
.dropzone--active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--drop-bg));
}

.dropzone__content {
  text-align: center;
  max-width: 420px;
}

.dropzone__icon {
  width: 48px;
  height: 48px;
  margin: 0 auto 1rem;
  color: var(--text-muted);
}

.dropzone__title {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
  font-weight: 600;
}

.dropzone__text {
  margin: 0 0 0.75rem;
  color: var(--text-muted);
}

.dropzone__hint {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--text-muted);
  opacity: 0.85;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  text-decoration: none;
  padding: 0;
}

.link-btn:hover {
  color: var(--accent-hover);
  text-decoration: underline;
}

/* Viewer */

.viewer {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--preview-bg);
}

.viewer__toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 1rem 1.25rem 0.75rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.viewer__tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  flex: 1;
  min-width: 0;
}

.tab {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.375rem 0.625rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.8125rem;
  color: var(--text-muted);
  cursor: pointer;
  max-width: 220px;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.tab:hover {
  color: var(--text);
  background: var(--drop-bg);
}

.tab--active {
  background: var(--accent-subtle);
  border-color: var(--accent);
  color: var(--text);
}

.tab__name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tab__close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 4px;
  flex-shrink: 0;
  padding: 0;
}

.tab__close:hover {
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
}

.tab__close svg {
  width: 12px;
  height: 12px;
}

.viewer__actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.select-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.select-group__label {
  font-size: 0.8125rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.select {
  appearance: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.8125rem;
  padding: 0.375rem 1.75rem 0.375rem 0.625rem;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M3 4.5L6 7.5L9 4.5' stroke='%238b949e' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.5rem center;
}

[data-theme="light"] .select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M3 4.5L6 7.5L9 4.5' stroke='%23656d76' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
}

.select:focus {
  outline: none;
  border-color: var(--accent);
}

.select--full {
  width: 100%;
}

.input {
  width: 100%;
  padding: 0.4375rem 0.625rem;
  font-family: inherit;
  font-size: 0.875rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.input:focus {
  outline: none;
  border-color: var(--accent);
}

.viewer__meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.25rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
  padding: 0.75rem 1.25rem;
}

.viewer__meta strong {
  color: var(--text);
  font-weight: 500;
}

.viewer__code-wrapper {
  flex: 1;
  display: flex;
  min-height: 0;
  margin: 0 1.25rem 1.25rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--code-bg);
  overflow: auto;
}

.line-numbers {
  padding: 1rem 0;
  text-align: right;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.45;
  color: var(--text-muted);
  user-select: none;
  border-right: 1px solid var(--border);
  min-width: 3.25rem;
  flex-shrink: 0;
  opacity: 0.7;
}

.line-numbers span {
  display: block;
  padding: 0 0.75rem;
}

.viewer__code {
  flex: 1;
  margin: 0;
  padding: 1rem 1.25rem;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.45;
  tab-size: 2;
}

.viewer__code code {
  white-space: pre;
}

/* Syntax highlighting */

.hl-keyword { color: var(--hl-keyword); font-weight: 600; }
.hl-type { color: var(--hl-type); }
.hl-string { color: var(--hl-string); }
.hl-number { color: var(--hl-number); }
.hl-comment { color: var(--hl-comment); font-style: italic; }
.hl-punctuation { color: var(--hl-punctuation); }

/* Drop overlay */

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--bg) 75%, transparent);
  border: 3px dashed var(--accent);
  pointer-events: none;
}

.drop-overlay__text {
  margin: 0;
  padding: 0.75rem 1.25rem;
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px color-mix(in srgb, var(--bg) 50%, transparent);
}

/* Footer */

.footer {
  padding: 0.75rem 1.25rem;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.footer p {
  margin: 0;
}

.footer__link {
  color: var(--accent);
  text-decoration: none;
}

.footer__link:hover {
  text-decoration: underline;
}

.footer__credit {
  margin: 0.375rem 0 0;
  font-size: 0.6875rem;
  opacity: 0.85;
}

/* Toast */

.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.625rem 1.25rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  box-shadow: 0 8px 32px color-mix(in srgb, var(--bg) 50%, transparent);
  z-index: 1000;
}

/* Modal */

.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal.hidden {
  display: none !important;
}

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: color-mix(in srgb, var(--bg) 60%, transparent);
  backdrop-filter: blur(2px);
}

.modal__dialog {
  position: relative;
  width: 100%;
  max-width: 440px;
  max-height: calc(100dvh - 2rem);
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) * 1.5);
  box-shadow: 0 16px 48px color-mix(in srgb, var(--bg) 60%, transparent);
}

.modal__dialog--wide {
  max-width: 560px;
}

.modal__dialog--tall {
  max-height: calc(100dvh - 2rem);
}

.modal__dialog--tall .modal__body {
  max-height: calc(100dvh - 12rem);
}

.markdown-doc {
  font-size: 0.9375rem;
  line-height: 1.6;
  word-wrap: break-word;
}

.markdown-doc h1 {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0 0 0.75rem;
  padding-bottom: 0.375rem;
  border-bottom: 1px solid var(--border);
}

.markdown-doc h2 {
  font-size: 1rem;
  font-weight: 600;
  margin: 1.25rem 0 0.5rem;
}

.markdown-doc h3 {
  font-size: 0.9375rem;
  font-weight: 600;
  margin: 1rem 0 0.375rem;
}

.markdown-doc p {
  margin: 0 0 0.75rem;
  color: var(--text);
}

.markdown-doc ul {
  margin: 0 0 0.75rem;
  padding-left: 1.25rem;
}

.markdown-doc li {
  margin-bottom: 0.25rem;
}

.markdown-doc strong {
  font-weight: 600;
  color: var(--text);
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.modal__title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.modal__body {
  padding: 1.25rem;
  overflow-y: auto;
}

.modal__footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);
}

.modal__note {
  margin: 0 0 1rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  margin-bottom: 1rem;
}

.field__label {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text);
}

.field__hint {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.field__error {
  margin: 0;
  font-size: 0.8125rem;
  color: var(--danger);
}

.explain-loading {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.explain-content {
  margin: 0;
  font-size: 0.9375rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Responsive */

@media (max-width: 600px) {
  .header__filename {
    display: none;
  }

  .dropzone {
    margin: 1rem;
    padding: 1.5rem;
  }

  .viewer__toolbar {
    flex-direction: column;
  }

  .viewer__actions {
    width: 100%;
  }

  .select-group {
    flex: 1;
  }

  .select {
    flex: 1;
  }

  .viewer__code-wrapper {
    margin: 0 1rem 1rem;
  }
}
