/* ── Variables ────────────────────────────────────────────────────────────── */
:root {
  --bg:        #f5f5f7;
  --surface:   #ffffff;
  --text:      #1a1a1a;
  --text-muted:#888;
  --border:    #e2e2e7;
  --accent:    #6366f1;
  --accent-fg: #ffffff;
  --btn-bg:    #f0f0f5;
  --btn-hover: #e4e4eb;
  --radius:    10px;
  --radius-sm: 7px;
  --input-bg:  #f0f0f5;
  --shadow:    0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #0d0d10;
    --surface:   #18181f;
    --text:      #ececf1;
    --text-muted:#666;
    --border:    #2a2a35;
    --accent:    #818cf8;
    --accent-fg: #ffffff;
    --btn-bg:    #222230;
    --btn-hover: #2c2c3c;
    --input-bg:  #222230;
    --shadow:    0 1px 3px rgba(0,0,0,.3);
  }
}

/* ── Reset ────────────────────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* ── Base ─────────────────────────────────────────────────────────────────── */
html {
  height: 100%;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

/* ── Header ───────────────────────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
  flex-shrink: 0;
}

/* ── Icon Buttons ─────────────────────────────────────────────────────────── */
.icon-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--btn-bg);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  pointer-events: none;
}

.icon-btn:hover:not(:disabled) {
  background: var(--btn-hover);
}

.icon-btn:active:not(:disabled) {
  transform: scale(0.94);
}

.icon-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

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

.icon-btn.primary:hover:not(:disabled) {
  filter: brightness(1.1);
}

/* ── Input ────────────────────────────────────────────────────────────────── */
#topic-input {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--input-bg);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
  -webkit-appearance: none;
}

#topic-input::placeholder {
  color: var(--text-muted);
}

#topic-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

/* ── Main ─────────────────────────────────────────────────────────────────── */
main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 20px 16px;
  padding-bottom: calc(20px + env(safe-area-inset-bottom));
}

/* ── Placeholder ─────────────────────────────────────────────────────────── */
.placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: calc(100dvh - 120px);
  color: var(--text-muted);
  text-align: center;
  user-select: none;
}

.placeholder svg {
  width: 48px;
  height: 48px;
  opacity: 0.4;
}

.placeholder p {
  font-size: 0.95rem;
}

/* ── Response Content ────────────────────────────────────────────────────── */
#response-content:not(.placeholder) {
  max-width: 720px;
  margin: 0 auto;
  font-size: 1rem;
  line-height: 1.75;
}

/* ── Footer ───────────────────────────────────────────────────────────────── */
footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
  letter-spacing: 0.01em;
}

footer .separator {
  opacity: 0.5;
}

footer .version {
  font-variant-numeric: tabular-nums;
}

/* ── Warning state ───────────────────────────────────────────────────────── */
:root {
  --warning: #d97706;
}

@media (prefers-color-scheme: dark) {
  :root {
    --warning: #fbbf24;
  }
}

.warning {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: calc(100dvh - 120px);
  color: var(--warning);
  text-align: center;
  user-select: none;
}

.warning svg {
  width: 48px;
  height: 48px;
  opacity: 0.75;
}

.warning p {
  font-size: 0.95rem;
  max-width: 300px;
  line-height: 1.5;
}

/* ── Dialog ───────────────────────────────────────────────────────────────── */
dialog {
  border: none;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,.25), 0 4px 16px rgba(0,0,0,.12);
  width: min(420px, calc(100vw - 32px));
  overflow: hidden;
}

dialog::backdrop {
  background: rgba(0,0,0,.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

dialog[open] {
  animation: dialog-in .15s ease-out;
}

@keyframes dialog-in {
  from { opacity: 0; transform: translateY(-8px) scale(.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 12px 20px;
  border-bottom: 1px solid var(--border);
}

.dialog-header h2 {
  font-size: 1rem;
  font-weight: 600;
}

.dialog-body {
  padding: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  padding: 12px 20px 16px;
  border-top: 1px solid var(--border);
}

/* ── Field ────────────────────────────────────────────────────────────────── */
.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.field input {
  height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--input-bg);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
  -webkit-appearance: none;
}

.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

.field-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.4;
}

/* ── Save button ──────────────────────────────────────────────────────────── */
#btn-save-key {
  height: 36px;
  padding: 0 20px;
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: filter .15s, transform .1s;
  -webkit-tap-highlight-color: transparent;
}

#btn-save-key:hover {
  filter: brightness(1.1);
}

#btn-save-key:active {
  transform: scale(.96);
}

/* ── Loading state ───────────────────────────────────────────────────────── */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: calc(100dvh - 120px);
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* ── Response paragraph ──────────────────────────────────────────────────── */
.response {
  max-width: 720px;
  margin: 0 auto;
  line-height: 1.75;
  font-size: 1rem;
}

/* ── Keyword links ───────────────────────────────────────────────────────── */
a.topic-link {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  cursor: pointer;
  transition: border-color 0.1s;
}

a.topic-link:hover {
  border-bottom-color: var(--accent);
}

/* ── Error state ─────────────────────────────────────────────────────────── */
:root { --error: #dc2626; }

@media (prefers-color-scheme: dark) { :root { --error: #f87171; } }

.error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: calc(100dvh - 120px);
  color: var(--error);
  text-align: center;
  user-select: none;
}

.error svg {
  width: 48px;
  height: 48px;
  opacity: 0.75;
}

.error p {
  font-size: 0.95rem;
  max-width: 300px;
  line-height: 1.5;
}

/* ── Dimmed paragraph ────────────────────────────────────────────────────── */
p.dimmed {
  color: var(--text-muted);
  transition: color 0.2s;
}

p.dimmed a.topic-link {
  color: color-mix(in srgb, var(--accent) 35%, var(--text-muted));
  border-bottom-color: transparent;
  pointer-events: none;
}

/* ── Expansion paragraph ─────────────────────────────────────────────────── */
p.expansion {
  margin-bottom: 1.25em;
  padding-bottom: 1.25em;
  border-bottom: 1px solid var(--border);
}

/* ── Collapse button ─────────────────────────────────────────────────────── */
.collapse-btn {
  display: inline-flex;
  align-items: center;
  margin-left: 8px;
  padding: 1px 8px;
  font-size: 0.72rem;
  color: var(--text-muted);
  background: var(--btn-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  vertical-align: middle;
  font-family: inherit;
  line-height: 1.6;
  transition: background 0.1s, color 0.1s;
  white-space: nowrap;
}

.collapse-btn:hover {
  background: var(--btn-hover);
  color: var(--text);
}

/* ── Inline spinner ──────────────────────────────────────────────────────── */
.spinner-sm {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}

/* ── Expansion error ─────────────────────────────────────────────────────── */
.expansion-error {
  color: var(--error);
}

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  header {
    gap: 6px;
    padding-left: 10px;
    padding-right: 10px;
  }
}

@media (min-width: 768px) {
  header {
    padding-left: 20px;
    padding-right: 20px;
  }

  main {
    padding: 32px 24px;
  }
}

@media (min-width: 1024px) {
  header {
    padding-left: max(24px, calc((100% - 960px) / 2));
    padding-right: max(24px, calc((100% - 960px) / 2));
  }

  main {
    padding: 40px max(24px, calc((100% - 720px) / 2));
  }
}
