:root {
  color-scheme: dark;

  /* Dark palette (no pure black) */
  --bg: #0d0d0d;
  /* darker grey background */
  --muted-bg: #1a1a1a;
  --surface: #272424;
  /* card/input */
  --fg: #eeeeee;
  /* text */
  --muted: #222222;
  /* secondary text */
  --muted-fg: #b4b4b4;
  --border: #242b36;
  /* subtle borders */
  --accent: #2a2a2a;
  /* calm teal */
  --accent-soft: rgba(45, 212, 191, 0.12);
  --ring: 0 0 0 4px color-mix(in oklab, var(--accent) 26%, transparent);

  --radius: 16px;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #ebebed;
  --muted-bg: #b4b5b7;
  --surface: #ecf4fb;
  --fg: #0f172a;
  --muted: #475569;
  --muted-fg: #64748b;
  --border: #cbd5e1;
  --accent: #22c55e;
  --accent-soft: rgba(34, 197, 94, 0.12);
  --ring: 0 0 0 4px color-mix(in oklab, var(--accent) 26%, transparent);
}

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

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    color-scheme: dark;
    /* Dark palette (no pure black) */
    --bg: #0d0d0d;
    /* darker grey background */
    --muted-bg: #1a1a1a;
    --surface: #272424;
    /* card/input */
    --fg: #eeeeee;
    /* text */
    --muted: #222222;
    /* secondary text */
    --muted-fg: #b4b4b4;
    --border: #242b36;
    /* subtle borders */
    --accent: #2a2a2a;
    /* calm teal */
    --accent-soft: rgba(45, 212, 191, 0.12);
    --ring: 0 0 0 4px color-mix(in oklab, var(--accent) 26%, transparent);
  }
}

@media (prefers-color-scheme: light) {
  :root[data-theme="system"] {
    color-scheme: light;
    --bg: #f8fafc;
    --muted-bg: #f3f4f6;
    --surface: #ffffff;
    --fg: #0f172a;
    --muted: #475569;
    --muted-fg: #64748b;
    --border: #e2e8f0;
    --accent: #22c55e;
    --accent-soft: rgba(34, 197, 94, 0.12);
    --ring: 0 0 0 4px color-mix(in oklab, var(--accent) 26%, transparent);
  }
}

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

html,
body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100svh;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto,
    Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  color: var(--fg);
  background: radial-gradient(1100px 800px at 20% 10%,
      rgba(45, 212, 191, 0.06),
      transparent 60%),
    radial-gradient(1000px 700px at 80% 90%,
      rgba(59, 130, 246, 0.05),
      transparent 60%),
    var(--bg);
  display: grid;
  place-items: center;
  overflow: hidden;
}

/* Layout */
.container {
  display: grid;
  place-items: center;
  width: 97%;
  height: 95%;
  border-radius: 1.5rem;
  background-color: var(--muted-bg);
}

.search-container {
  position: relative;
  width: min(720px, 92vw);
  display: grid;
  justify-items: center;
  gap: 16px;
  padding-top: 40px;
}

/* Search */
.search {
  width: 100%;
}

.search-wrap {
  position: relative;
  width: 100%;
}

.search-bar {
  width: 100%;
  height: clamp(50px, 8.5vh, 64px);
  padding: 0 22px;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: clamp(16px, 2.4vw, 18px);
  outline: none;
  transition: border-color 160ms ease, box-shadow 160ms ease,
    background 160ms ease;
  font-variant-numeric: normal;
  caret-color: var(--fg);
}

.search-bar::placeholder {
  color: var(--muted-fg);
}

.search-bar:focus-visible {
  border-color: var(--accent);
  box-shadow: var(--ring);
}

/* Hide legacy inline ghost (no longer used) */
.calc-ghost {
  display: none !important;
}

/* Separate calc panel */
.calc-panel {
  display: none;
  width: 100%;
  margin-top: 10px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: color-mix(in oklab, var(--accent) 12%, var(--surface));
  color: var(--fg);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  transition: background 160ms ease, border-color 160ms ease,
    box-shadow 160ms ease, transform 120ms ease;
}

.calc-panel:not([hidden]) {
  display: block;
}

.calc-panel:hover {
  transform: translateY(-1px);
}

.calc-expr {
  color: var(--muted-fg);
  font-size: 13px;
  line-height: 1.2;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.calc-out {
  font-size: clamp(22px, 4vw, 30px);
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1, "lnum" 1;
}

/* Hints */
.hints {
  text-align: center;
  color: var(--muted-fg);
  font-size: 12.5px;
  letter-spacing: 0.2px;
}

/* Dialog */
.help {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--fg);
  padding: 16px 18px;
  width: min(700px, 92vw);
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
}

.dialog-content {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
  overflow: auto;
  padding-right: 4px;
}

@media (min-width: 640px) {
  .dialog-content {
    grid-template-columns: 1fr;
    row-gap: 12px;
  }
}

.dialog-content>hr {
  grid-column: 1 / -1;
  height: 1px;
  border: 0;
  background: var(--border);
  margin: 2px 0 6px;
}

/* Section blocks */
.dialog-content>.shortcuts,
.dialog-content>.prefix {
  display: grid;
  align-content: start;
  gap: 6px;
  min-width: 0;
}

.help h2 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0.2px;
}

.help h4 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted-fg);
}

/* Lists */
.help ul {
  margin: 0;
  padding-left: 1.1em;
  line-height: 1.7;
  color: var(--muted-fg);
}

.help li {
  margin: 0 0 4px 0;
}

/* Close button aligned to end */
.help>.btn {
  display: block;
  margin: 10px 0 0 auto;
}

/* Backdrop */
.help::backdrop {
  background: rgba(0, 0, 0, 0.32);
}

/* Button */
.btn {
  background: color-mix(in oklab, var(--accent) 14%, var(--surface));
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font: inherit;
  cursor: pointer;
  transition: box-shadow 150ms ease, border-color 150ms ease,
    background 150ms ease;
}

.btn:focus-visible {
  outline: none;
  box-shadow: var(--ring);
  border-color: var(--accent);
}

/* Visually hidden */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}

/* Optional local font */
@font-face {
  font-family: "SerifHead";
  src: url("./fonts/serif-head.woff2") format("woff2");
  font-display: swap;
}

.quote {
  width: 100%;
  font-family: "SerifHead", ui-serif, Georgia, Cambria, "Times New Roman", serif;
  font-size: clamp(18px, 3.2vw, 28px);
  font-weight: 500;
  letter-spacing: 0.3px;
  color: var(--fg);
  text-align: end;
  /* padding: 4px 12px 10px; */
  opacity: 0.92;
  user-select: none;
}

.quote cite.author {
  display: inline-block;
  margin-left: 8px;
  font-size: 0.75em;
  font-style: italic;
  color: var(--muted-fg);
}

/* Micro toast */
.toast {
  position: fixed;
  inset-inline: 0;
  bottom: 18px;
  margin-inline: auto;
  width: fit-content;
  max-width: 92vw;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: color-mix(in oklab, var(--accent) 14%, var(--surface));
  color: var(--fg);
  font-size: 13px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 160ms ease, transform 160ms ease;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

/* Settings button */
.theme-btn,
.settings-btn {
  position: absolute;
  top: -10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  color: var(--muted-fg);
  cursor: pointer;
  transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  z-index: 10;
}

.theme-btn {
  right: 56px;
  border-radius: 10px;
  font-size: 18px;
}

.settings-btn {
  right: 0;
  clip-path: polygon(18% 0,
      100% 0,
      100% 100%,
      0 100%,
      0 22%);
}

.settings-btn svg {
  width: 20px;
  height: 20px;
  transition: transform 300ms cubic-bezier(0.4, 0, 0.2, 1);
}

.settings-btn:hover svg {
  transform: rotate(90deg);
}

.theme-btn:hover,
.settings-btn:hover {
  background: color-mix(in oklab, var(--accent) 20%, var(--surface));
  color: var(--fg);
  border-color: var(--accent);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  transform: translateY(-1px);
}

.theme-btn:active,
.settings-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.theme-btn:focus-visible,
.settings-btn:focus-visible {
  outline: none;
  box-shadow: var(--ring);
  border-color: var(--accent);
}

.settings-btn:focus-visible {
  outline: none;
  box-shadow: var(--ring);
  border-color: var(--accent);
}

/* Settings Dialog */
.settings-dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--fg);
  padding: 24px;
  width: min(600px, 92vw);
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
}

.settings-dialog::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

.settings-dialog h2 {
  margin: 0 0 20px 0;
  font-size: 24px;
}

.settings-content {
  display: grid;
  gap: 24px;
}

.settings-section h3 {
  margin: 0 0 10px 0;
  font-size: 16px;
  color: var(--fg);
}

.settings-section small {
  display: block;
  margin-top: 6px;
  color: var(--muted-fg);
  font-size: 12px;
}

/* Form elements */
.settings-select,
.settings-textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--fg);
  font-family: inherit;
  font-size: 14px;
  transition: border-color 160ms ease, box-shadow 160ms ease;
}

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

.settings-textarea {
  resize: none;
  min-height: 80px;
  font-family: ui-monospace, monospace;
  line-height: 1.5;
}

.settings-textarea::-webkit-scrollbar {
  width: 6px;
}

.settings-textarea::-webkit-scrollbar-thumb {
  background: var(--muted);
  border-radius: 10px;
}

/* Actions */
.settings-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 24px;
  gap: 12px;
  flex-wrap: wrap;
}

.settings-actions>div {
  display: flex;
  gap: 10px;
}

.btn-primary {
  background: color-mix(in oklab, var(--accent) 40%, var(--surface));
  font-weight: 600;
}

.btn-secondary {
  background: var(--bg);
}

.btn-primary:hover {
  background: color-mix(in oklab, var(--accent) 50%, var(--surface));
}

/* Lifeline Counter */
.lifeline-counter {
  width: 100%;
  margin-top: 24px;
  padding: 16px 20px;
  background: color-mix(in oklab, var(--accent) 8%, var(--surface));
  border: 1px solid var(--border);
  border-radius: 14px;
  text-align: center;
  opacity: 0.85;
  transition: opacity 200ms ease, transform 200ms ease;
}

.lifeline-counter:hover {
  opacity: 1;
  transform: translateY(-1px);
}

.lifeline-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted-fg);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 10px;
}

.lifeline-time {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: clamp(8px, 2vw, 16px);
  flex-wrap: wrap;
}

.time-segment {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 50px;
}

.time-value {
  font-size: clamp(20px, 3.5vw, 28px);
  font-weight: 700;
  color: var(--fg);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.time-unit {
  font-size: 11px;
  color: var(--muted-fg);
  text-transform: lowercase;
  font-weight: 500;
}

/* Responsive adjustments */
@media (max-width: 640px) {
  .settings-btn {
    width: 40px;
    height: 40px;
    top: -8px;
  }

  .settings-btn svg {
    width: 18px;
    height: 18px;
  }

  .search-container {
    padding-top: 36px;
  }

  .lifeline-counter {
    padding: 12px 16px;
    margin-top: 20px;
  }

  .time-segment {
    min-width: 45px;
  }
}

@media (max-width: 420px) {
  .calc-ghost {
    font-size: clamp(16px, 5vw, 20px);
  }

  .settings-btn {
    width: 38px;
    height: 38px;
    top: -6px;
  }

  .settings-btn svg {
    width: 16px;
    height: 16px;
  }

  .search-container {
    padding-top: 32px;
  }

  .lifeline-counter {
    padding: 10px 12px;
    margin-top: 16px;
  }

  .lifeline-label {
    font-size: 10px;
    margin-bottom: 8px;
  }

  .lifeline-time {
    gap: 6px;
  }

  .time-segment {
    min-width: 40px;
    gap: 2px;
  }

  .time-value {
    font-size: clamp(16px, 4vw, 20px);
  }

  .time-unit {
    font-size: 9px;
  }
}