/* Design tokens. Both themes define the same names so components never
   branch on theme; only the values below change. */

:root {
  color-scheme: light dark;

  /* Type */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  /* Scale: 1.200 ratio, rounded to whole px */
  --text-xs: 0.75rem;
  --text-sm: 0.8125rem;
  --text-base: 0.9375rem;
  --text-md: 1.0625rem;
  --text-lg: 1.3125rem;
  --text-xl: 1.75rem;

  /* Spacing: 4px base */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;

  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  --radius-pill: 999px;

  --ring-width: 3px;
  --tap-target: 44px;

  --speed-fast: 0.12s;
  --speed: 0.2s;
  --ease: cubic-bezier(0.32, 0.72, 0, 1);
}

/* ---------------------------------------------------------------- dark */

:root,
:root[data-theme="dark"] {
  --bg: #0c0f16;
  --bg-glow-1: #1d2a44;
  --bg-glow-2: #141a27;
  --surface: #151a24;
  --surface-2: #1c2230;
  --surface-3: #232a3a;
  --surface-sunken: #10141c;

  --line: #2a3242;
  --line-strong: #3a4458;

  --text: #e8ebf1;
  --text-dim: #a2abbd;
  --text-faint: #6e7889;

  --accent: #6c9bff;
  --accent-hover: #83adff;
  --accent-ink: #0a0d14;
  --accent-wash: rgba(108, 155, 255, 0.14);
  --accent-ring: rgba(108, 155, 255, 0.4);

  --ok: #42d79a;
  --ok-wash: rgba(66, 215, 154, 0.12);
  --ok-line: rgba(66, 215, 154, 0.34);
  --ok-text: #8fe8c2;

  --warn: #f7b44c;
  --warn-wash: rgba(247, 180, 76, 0.12);
  --warn-line: rgba(247, 180, 76, 0.34);
  --warn-text: #ffd89b;

  --err: #ff7a7a;
  --err-wash: rgba(255, 122, 122, 0.12);
  --err-line: rgba(255, 122, 122, 0.36);
  --err-text: #ffb4b4;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.55);
  --overlay: rgba(0, 0, 0, 0.28);
  --skeleton: linear-gradient(
    90deg,
    var(--surface-2) 25%,
    var(--surface-3) 37%,
    var(--surface-2) 63%
  );
}

/* --------------------------------------------------------------- light */

:root[data-theme="light"] {
  --bg: #f7f8fa;
  --bg-glow-1: #e4ebfa;
  --bg-glow-2: #eef1f6;
  --surface: #ffffff;
  --surface-2: #f4f6f9;
  --surface-3: #e9edf3;
  --surface-sunken: #f9fafc;

  --line: #e2e6ed;
  --line-strong: #cbd2dd;

  --text: #1a1f29;
  --text-dim: #5c6676;
  --text-faint: #8a93a3;

  --accent: #2f6bed;
  --accent-hover: #2559d4;
  --accent-ink: #ffffff;
  --accent-wash: rgba(47, 107, 237, 0.08);
  --accent-ring: rgba(47, 107, 237, 0.3);

  --ok: #0f9d63;
  --ok-wash: rgba(15, 157, 99, 0.09);
  --ok-line: rgba(15, 157, 99, 0.28);
  --ok-text: #0a7d4e;

  --warn: #b26a00;
  --warn-wash: rgba(178, 106, 0, 0.09);
  --warn-line: rgba(178, 106, 0, 0.26);
  --warn-text: #8a5200;

  --err: #d93535;
  --err-wash: rgba(217, 53, 53, 0.07);
  --err-line: rgba(217, 53, 53, 0.26);
  --err-text: #b02525;

  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow: 0 4px 14px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 16px 40px rgba(16, 24, 40, 0.12);
  --overlay: rgba(255, 255, 255, 0.6);
  --skeleton: linear-gradient(
    90deg,
    var(--surface-2) 25%,
    var(--surface-3) 37%,
    var(--surface-2) 63%
  );
}

/* Follow the OS when the user has not picked a theme. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg: #f7f8fa;
    --bg-glow-1: #e4ebfa;
    --bg-glow-2: #eef1f6;
    --surface: #ffffff;
    --surface-2: #f4f6f9;
    --surface-3: #e9edf3;
    --surface-sunken: #f9fafc;

    --line: #e2e6ed;
    --line-strong: #cbd2dd;

    --text: #1a1f29;
    --text-dim: #5c6676;
    --text-faint: #8a93a3;

    --accent: #2f6bed;
    --accent-hover: #2559d4;
    --accent-ink: #ffffff;
    --accent-wash: rgba(47, 107, 237, 0.08);
    --accent-ring: rgba(47, 107, 237, 0.3);

    --ok: #0f9d63;
    --ok-wash: rgba(15, 157, 99, 0.09);
    --ok-line: rgba(15, 157, 99, 0.28);
    --ok-text: #0a7d4e;

    --warn: #b26a00;
    --warn-wash: rgba(178, 106, 0, 0.09);
    --warn-line: rgba(178, 106, 0, 0.26);
    --warn-text: #8a5200;

    --err: #d93535;
    --err-wash: rgba(217, 53, 53, 0.07);
    --err-line: rgba(217, 53, 53, 0.26);
    --err-text: #b02525;

    --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
    --shadow: 0 4px 14px rgba(16, 24, 40, 0.08);
    --shadow-lg: 0 16px 40px rgba(16, 24, 40, 0.12);
    --overlay: rgba(255, 255, 255, 0.6);
  }
}

/* ---------------------------------------------------------------- reset */

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

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--bg);
  background-image: radial-gradient(
      900px 520px at 50% -15%,
      var(--bg-glow-1) 0%,
      transparent 70%
    ),
    radial-gradient(700px 400px at 100% 0%, var(--bg-glow-2) 0%, transparent 60%);
  background-repeat: no-repeat;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  transition: background-color var(--speed) var(--ease), color var(--speed) var(--ease);
}

h1,
h2,
h3 {
  margin: 0;
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: -0.015em;
}

p {
  margin: 0;
}

a {
  color: var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

a:hover {
  color: var(--accent-hover);
}

/* One visible focus treatment everywhere, keyboard-only. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

:focus:not(:focus-visible) {
  outline: none;
}

/* ------------------------------------------------------------- layout */

.wrap {
  max-width: 820px;
  margin: 0 auto;
  padding: var(--space-7) var(--space-4) var(--space-7);
}

.wrap-wide {
  max-width: 1240px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  box-shadow: var(--shadow-sm);
}

.muted {
  color: var(--text-dim);
}

.mono {
  font-family: var(--font-mono);
  font-variant-ligatures: none;
}

/* Tabular figures keep columns of numbers aligned. */
.num {
  font-variant-numeric: tabular-nums;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* Visible only once focused — the skip link. */
.skip-link {
  position: absolute;
  left: var(--space-4);
  top: calc(var(--space-4) * -4);
  z-index: 100;
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-4);
  color: var(--text);
  text-decoration: none;
  transition: top var(--speed) var(--ease);
}

.skip-link:focus {
  top: var(--space-4);
}

/* ------------------------------------------------------------ controls */

button,
.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: 36px;
  padding: var(--space-2) var(--space-4);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: var(--text-sm);
  text-decoration: none;
  cursor: pointer;
  transition: background var(--speed-fast) var(--ease),
    border-color var(--speed-fast) var(--ease), transform var(--speed-fast) var(--ease),
    opacity var(--speed-fast) var(--ease);
}

button:hover:not(:disabled),
.btn:hover {
  background: var(--surface-3);
  border-color: var(--accent);
}

button:active:not(:disabled),
.btn:active {
  transform: translateY(1px);
}

button:disabled,
button[aria-disabled="true"] {
  opacity: 0.45;
  cursor: not-allowed;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

button.primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-dim);
}

button.ghost:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: var(--line);
  color: var(--text);
}

button.danger {
  color: var(--err-text);
  border-color: var(--err-line);
}

button.danger:hover:not(:disabled) {
  background: var(--err-wash);
  border-color: var(--err);
}

button.sm,
.btn.sm {
  min-height: 28px;
  padding: var(--space-1) var(--space-3);
  font-size: var(--text-xs);
}

input,
select,
textarea {
  width: 100%;
  min-height: 38px;
  padding: var(--space-2) var(--space-3);
  background: var(--surface-sunken);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  color: var(--text);
  font: inherit;
  font-size: var(--text-base);
  transition: border-color var(--speed-fast) var(--ease),
    box-shadow var(--speed-fast) var(--ease);
}

input::placeholder,
textarea::placeholder {
  color: var(--text-faint);
}

input:hover:not(:disabled),
select:hover:not(:disabled),
textarea:hover:not(:disabled) {
  border-color: var(--line-strong);
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 var(--ring-width) var(--accent-ring);
}

input[aria-invalid="true"] {
  border-color: var(--err);
}

input[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 var(--ring-width) var(--err-wash);
}

select {
  appearance: none;
  padding-right: var(--space-6);
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 16px) calc(50% + 2px),
    calc(100% - 11px) calc(50% + 2px);
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

textarea {
  resize: vertical;
  font-size: var(--text-sm);
  line-height: 1.55;
}

input[type="checkbox"] {
  width: auto;
  min-height: 0;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

input[type="file"] {
  padding: var(--space-2);
  font-size: var(--text-sm);
  cursor: pointer;
}

label {
  display: block;
  margin-bottom: var(--space-2);
  color: var(--text-dim);
  font-size: var(--text-sm);
  font-weight: 500;
}

.field {
  margin-bottom: var(--space-4);
}

.field-hint {
  margin-top: var(--space-2);
  color: var(--text-faint);
  font-size: var(--text-xs);
}

.row {
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.row > * {
  flex: 1 1 160px;
  min-width: 0;
}

/* -------------------------------------------------------------- alerts */

.alert {
  display: flex;
  align-items: flex-start;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: var(--text-sm);
  line-height: 1.5;
}

.alert::before {
  flex-shrink: 0;
  font-size: var(--text-base);
  line-height: 1.4;
}

.alert-ok {
  background: var(--ok-wash);
  border-color: var(--ok-line);
  color: var(--ok-text);
}

.alert-ok::before {
  content: "✓";
}

.alert-warn {
  background: var(--warn-wash);
  border-color: var(--warn-line);
  color: var(--warn-text);
}

.alert-warn::before {
  content: "!";
  font-weight: 700;
}

.alert-err {
  background: var(--err-wash);
  border-color: var(--err-line);
  color: var(--err-text);
}

.alert-err::before {
  content: "×";
  font-weight: 700;
}

/* ---------------------------------------------------------------- tags */

.tag {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 1px var(--space-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: var(--text-xs);
  white-space: nowrap;
}

.tag::before {
  content: "";
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.8;
}

.tag-ok {
  background: var(--ok-wash);
  border-color: var(--ok-line);
  color: var(--ok-text);
}

.tag-err {
  background: var(--err-wash);
  border-color: var(--err-line);
  color: var(--err-text);
}

.tag-warn {
  background: var(--warn-wash);
  border-color: var(--warn-line);
  color: var(--warn-text);
}

/* -------------------------------------------------------------- tables */

.table-scroll {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  /* Let a horizontally scrollable table be reached by keyboard. */
  scrollbar-width: thin;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

th,
td {
  padding: var(--space-3);
  text-align: left;
  white-space: nowrap;
  border-bottom: 1px solid var(--line);
}

thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr {
  transition: background var(--speed-fast) var(--ease);
}

tbody tr:hover {
  background: var(--surface-2);
}

/* ----------------------------------------------------------------- code */

pre {
  margin: 0;
  padding: var(--space-4);
  background: var(--surface-sunken);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  line-height: 1.6;
  tab-size: 2;
}

code {
  font-family: var(--font-mono);
  font-size: 0.95em;
}

/* ------------------------------------------------------------ skeleton */

.skeleton {
  border-radius: var(--radius-sm);
  background-image: var(--skeleton);
  background-size: 400% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}

@keyframes shimmer {
  0% {
    background-position: 100% 0;
  }
  100% {
    background-position: -100% 0;
  }
}

/* --------------------------------------------------------- theme toggle */

.theme-toggle {
  min-height: 34px;
  width: 34px;
  padding: 0;
  border-radius: var(--radius);
  font-size: var(--text-md);
  line-height: 1;
}

.theme-toggle .icon-sun {
  display: none;
}

.theme-toggle .icon-moon {
  display: block;
}

:root[data-theme="light"] .theme-toggle .icon-sun {
  display: block;
}

:root[data-theme="light"] .theme-toggle .icon-moon {
  display: none;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) .theme-toggle .icon-sun {
    display: block;
  }

  :root:not([data-theme]) .theme-toggle .icon-moon {
    display: none;
  }
}

/* --------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: var(--space-5);
  transform: translateX(-50%);
  z-index: 60;
  max-width: calc(100vw - var(--space-6));
  padding: var(--space-3) var(--space-5);
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-lg);
  color: var(--text);
  font-size: var(--text-sm);
  animation: toast-in var(--speed) var(--ease);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

/* --------------------------------------------------------------- empty */

.empty {
  padding: var(--space-6) var(--space-4);
  text-align: center;
  color: var(--text-dim);
  font-size: var(--text-sm);
}

.empty-icon {
  display: block;
  margin: 0 auto var(--space-2);
  font-size: var(--text-xl);
  opacity: 0.4;
}

.empty-hint {
  margin-top: var(--space-1);
  color: var(--text-faint);
  font-size: var(--text-xs);
}

/* ------------------------------------------------------ accessibility */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

@media (prefers-contrast: more) {
  :root {
    --line: var(--line-strong);
    --text-dim: var(--text);
  }
}

/* Touch devices: respect the 44px minimum tap target. */
@media (pointer: coarse) {
  button,
  .btn {
    min-height: var(--tap-target);
  }

  button.sm,
  .btn.sm {
    min-height: 34px;
  }

  input,
  select {
    min-height: var(--tap-target);
  }
}
