:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --card: #f8fafc;
  --accent: #6366f1;
  --accent-fg: #ffffff;
  --danger: #dc2626;
  --success: #059669;
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "SF Pro Text", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --fg: #f1f5f9;
    --muted: #94a3b8;
    --border: #1e293b;
    --card: #111827;
    --accent: #818cf8;
    --accent-fg: #0f172a;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  min-height: 100vh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.tabs {
  display: flex;
  gap: 0.25rem;
  background: var(--card);
  border: 1px solid var(--border);
  padding: 0.25rem;
  border-radius: 10px;
}
.tab {
  padding: 0.4rem 0.9rem;
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.9rem;
  border-radius: 8px;
  cursor: pointer;
}
.tab.active {
  background: var(--bg);
  color: var(--fg);
  border-color: var(--border);
}

#chat {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1rem;
  min-height: 4rem;
}

#ask-view:not([hidden]) {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

body.ask-active {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}
body.ask-active main {
  height: 100%;
  padding-bottom: 0;
  gap: 1rem;
}
body.ask-active #ask-view {
  flex: 1;
  min-height: 0;
}
body.ask-active #chat {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  margin-bottom: 0;
  padding-bottom: 0.5rem;
}
body.ask-active #ask-form {
  padding-bottom: env(safe-area-inset-bottom);
}

#chat .turn {
  padding: 0.875rem 1rem;
  border-radius: 12px;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.5;
}

#chat .turn.user {
  background: var(--accent);
  color: var(--accent-fg);
  align-self: flex-end;
  max-width: 90%;
}

#chat .turn.assistant {
  background: var(--card);
  border: 1px solid var(--border);
  align-self: flex-start;
  max-width: 100%;
}

#chat .turn.assistant.streaming::after {
  content: "▌";
  opacity: 0.5;
  animation: blink 1s steps(2) infinite;
}

@keyframes blink {
  to { opacity: 0; }
}

#chat .empty {
  color: var(--muted);
  text-align: center;
  padding: 1.5rem 0;
  font-size: 0.875rem;
}

#ask-form textarea {
  margin-bottom: 0;
}

h1 { font-size: 1.5rem; margin: 0; letter-spacing: -0.02em; }
h2 { font-size: 1rem; margin: 0; color: var(--muted); font-weight: 600; }

textarea {
  width: 100%;
  font: inherit;
  font-size: 1.0625rem;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card);
  color: var(--fg);
  resize: vertical;
  min-height: 8rem;
}
textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.row {
  display: flex;
  gap: 0.75rem;
  margin-top: 0.75rem;
  align-items: center;
}

button {
  font: inherit;
  font-size: 1rem;
  padding: 0.75rem 1.25rem;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: transform 0.05s ease, background 0.15s ease;
}
button:active { transform: translateY(1px); }

button.primary {
  background: var(--accent);
  color: var(--accent-fg);
  flex: 1;
  font-weight: 600;
}
button.primary:disabled { opacity: 0.5; cursor: not-allowed; }

button.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--fg);
  min-width: 3rem;
}
button.ghost.active {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}

.status {
  min-height: 1.25rem;
  margin: 0.5rem 0 0;
  font-size: 0.875rem;
  color: var(--muted);
}
.status.success { color: var(--success); }
.status.error { color: var(--danger); }

.recent-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

#thoughts {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#thoughts li {
  background: var(--card);
  border: 1px solid var(--border);
  padding: 0.875rem 1rem;
  border-radius: 10px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

#thoughts li time {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

#thoughts .empty {
  color: var(--muted);
  text-align: center;
  padding: 2rem 0;
  font-size: 0.875rem;
}

dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--fg);
  padding: 1.5rem;
  max-width: 22rem;
  width: calc(100% - 2rem);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }

dialog label {
  display: block;
  font-size: 0.875rem;
  color: var(--muted);
  margin-top: 1rem;
}

dialog input {
  display: block;
  width: 100%;
  font: inherit;
  margin-top: 0.25rem;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--fg);
}

.hint { font-size: 0.75rem; color: var(--muted); margin: 0.5rem 0 1rem; }

button.wide {
  display: block;
  width: 100%;
  text-align: left;
  margin: 0.75rem 0 0;
}

dialog#siri-help {
  max-width: 32rem;
}

.steps {
  margin: 0.75rem 0 1rem;
  padding-left: 1.25rem;
  font-size: 0.9375rem;
  line-height: 1.5;
}
.steps li { margin-bottom: 0.875rem; }
.steps li:last-child { margin-bottom: 0; }

.kv {
  display: grid;
  grid-template-columns: 5rem 1fr;
  gap: 0.5rem;
  margin: 0.4rem 0;
  font-size: 0.8125rem;
  align-items: start;
}
.kv > span:first-child {
  color: var(--muted);
  font-weight: 600;
  padding-top: 0.15rem;
}

code {
  background: var(--card);
  border: 1px solid var(--border);
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  font-size: 0.8125rem;
  word-break: break-all;
}

button.copy {
  font-size: 0.7rem;
  padding: 0.2rem 0.5rem;
  margin-left: 0.4rem;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 6px;
  cursor: pointer;
}
button.copy:active { background: var(--accent); color: var(--accent-fg); }
button.copy.copied { background: var(--success); color: white; border-color: var(--success); }
