/* Assistent: the terminal web app. Mobile first; light and dark follow the system. */

:root {
  color-scheme: light dark;
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #eef0f6;
  --text: #15171c;
  --muted: #5b6170;
  --border: #d9dce5;
  --accent: #2563eb;
  --accent-strong: #1d4ed8;
  --accent-text: #ffffff;
  --user-bg: #2563eb;
  --user-text: #ffffff;
  --bot-bg: #ffffff;
  --danger: #c62828;
  --danger-bg: #fdecec;
  --ok: #15803d;
  --warn: #b45309;
  --focus: #f59e0b;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 4px 16px rgb(0 0 0 / 0.06);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --surface: #1a1d24;
    --surface-2: #232733;
    --text: #e8eaf0;
    --muted: #9aa1b2;
    --border: #2f3442;
    --accent: #5b8cff;
    --accent-strong: #7aa2ff;
    --accent-text: #0b1020;
    --user-bg: #2f5fe0;
    --user-text: #ffffff;
    --bot-bg: #1f232c;
    --danger: #ff7b7b;
    --danger-bg: #3a1d1f;
    --ok: #4ade80;
    --warn: #fbbf24;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 4px 16px rgb(0 0 0 / 0.3);
  }
}

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

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}

[hidden] {
  display: none !important;
}

h1,
h2,
h3 {
  line-height: 1.2;
  margin: 0;
}

code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.92em;
  background: var(--surface-2);
  padding: 0.05em 0.35em;
  border-radius: 4px;
}

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

.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0.35rem 0 0;
}

.center {
  text-align: center;
}

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

.skip-link {
  position: absolute;
  left: 8px;
  top: -100px;
  z-index: 100;
  background: var(--surface);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 8px;
}

.skip-link:focus {
  top: calc(8px + var(--safe-top));
}

body:not([data-screen="chat"]) .skip-link {
  display: none;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* ---- Screens --------------------------------------------------------------------------- */

.screen {
  height: 100vh;
  height: 100dvh;
}

.screen--center {
  display: grid;
  place-items: center;
  padding: calc(24px + var(--safe-top)) calc(16px + var(--safe-right)) calc(24px + var(--safe-bottom)) calc(16px + var(--safe-left));
  overflow-y: auto;
}

.brand {
  text-align: center;
}

.brand img {
  border-radius: 18px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  padding: 28px 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-card h1 {
  font-size: 1.5rem;
}

.auth-card p {
  margin: 0;
}

.auth-logo {
  border-radius: 16px;
  align-self: flex-start;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

label {
  font-weight: 600;
  font-size: 0.95rem;
}

input[type="text"],
input[type="password"],
textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
  width: 100%;
}

input:focus-visible,
textarea:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 0;
  border-color: transparent;
}

.form-error {
  margin: 0;
  color: var(--danger);
  background: var(--danger-bg);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 0.92rem;
}

/* ---- Buttons --------------------------------------------------------------------------- */

.btn {
  font: inherit;
  font-weight: 600;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn:hover:not(:disabled) {
  filter: brightness(0.96);
}

.btn:disabled {
  opacity: 0.55;
  cursor: default;
}

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

.btn--danger {
  color: var(--danger);
  border-color: currentColor;
  background: transparent;
}

.btn--ghost {
  background: transparent;
  border-color: transparent;
}

.btn--block {
  width: 100%;
}

.btn--small {
  min-height: 36px;
  padding: 0 12px;
  font-size: 0.88rem;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.icon-btn {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 0;
  background: transparent;
  color: var(--text);
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
}

.icon-btn svg {
  width: 24px;
  height: 24px;
  fill: currentColor;
}

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

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

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

.chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(8px + var(--safe-top)) calc(8px + var(--safe-right)) 8px calc(14px + var(--safe-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-logo {
  border-radius: 9px;
}

.topbar-title {
  flex: 1;
  min-width: 0;
}

.topbar h1 {
  font-size: 1.05rem;
}

.conn {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn);
  flex: none;
}

.conn[data-state="open"] .conn-dot {
  background: var(--ok);
}

.conn[data-state="waiting"] .conn-dot,
.conn[data-state="stopped"] .conn-dot {
  background: var(--danger);
}

.conn[data-state="connecting"] .conn-dot {
  animation: pulse 1.2s ease-in-out infinite;
}

.messages-wrap {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px calc(12px + var(--safe-right)) 8px calc(12px + var(--safe-left));
}

.messages {
  list-style: none;
  margin: 0 auto;
  padding: 0;
  max-width: 820px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty {
  text-align: center;
  margin-top: 30vh;
  padding: 0 24px;
}

.msg {
  display: flex;
}

.msg--user {
  justify-content: flex-end;
}

.bubble {
  max-width: min(85%, 640px);
  padding: 9px 13px 6px;
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}

.msg--user .bubble {
  background: var(--user-bg);
  color: var(--user-text);
  border-bottom-right-radius: 6px;
}

.msg--assistant .bubble {
  background: var(--bot-bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.msg-text {
  margin: 0;
  white-space: pre-wrap;
}

.is-placeholder .msg-text {
  font-style: italic;
  opacity: 0.8;
}

.msg-error {
  margin: 4px 0 0;
  color: var(--danger);
  font-size: 0.92rem;
}

.msg-meta {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  font-size: 0.72rem;
  opacity: 0.7;
  margin-top: 2px;
}

.msg-meta:empty {
  display: none;
}

.is-interrupted .msg-status,
.is-failed .msg-status {
  font-weight: 600;
}

.is-failed .bubble {
  border-color: var(--danger);
}

.msg--assistant.is-streaming .msg-text::after {
  content: "▍";
  margin-left: 1px;
  animation: blink 1s steps(2, start) infinite;
}

.typing {
  display: inline-flex;
  gap: 4px;
  padding: 6px 0;
}

.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: bounce 1.2s infinite ease-in-out;
}

.typing span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing span:nth-child(3) {
  animation-delay: 0.3s;
}

.msg--assistant.is-streaming .msg-text:has(.typing)::after {
  content: none;
}

/* ---- Composer -------------------------------------------------------------------------- */

.composer-area {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 8px calc(10px + var(--safe-right)) calc(8px + var(--safe-bottom)) calc(10px + var(--safe-left));
}

.composer-area > * {
  max-width: 820px;
  margin-left: auto;
  margin-right: auto;
}

.activity {
  margin-top: 0;
  margin-bottom: 6px;
  font-size: 0.85rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}

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

.recording {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  font-size: 0.9rem;
}

.rec-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--danger);
  animation: pulse 1s ease-in-out infinite;
  flex: none;
}

.meter {
  flex: 1;
  height: 8px;
  border-radius: 4px;
  background: var(--surface-2);
  overflow: hidden;
}

.meter-fill {
  display: block;
  height: 100%;
  width: 100%;
  background: var(--accent);
  transform-origin: left center;
  transform: scaleX(0);
  transition: transform 60ms linear;
}

.rec-time {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 180px;
  border-radius: 22px;
  padding: 10px 16px;
  line-height: 1.4;
  background: var(--surface-2);
  border-color: transparent;
}

.icon-btn--send,
.icon-btn--mic {
  background: var(--accent);
  color: var(--accent-text);
}

.icon-btn--send:hover:not(:disabled),
.icon-btn--mic:hover:not(:disabled) {
  background: var(--accent-strong);
}

.icon-btn--mic {
  width: 52px;
  height: 52px;
  margin-bottom: -4px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.icon-btn--mic.is-recording {
  background: var(--danger);
  color: #fff;
  box-shadow: 0 0 0 6px color-mix(in srgb, var(--danger) 25%, transparent);
  animation: ring 1.2s ease-in-out infinite;
}

.icon-btn--stop {
  border: 2px solid var(--border);
}

.composer-options {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 6px;
  font-size: 0.85rem;
}

.kbd-hint {
  font-size: 0.78rem;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  cursor: pointer;
  min-height: 32px;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
}

.switch-track {
  width: 36px;
  height: 20px;
  border-radius: 10px;
  background: var(--border);
  position: relative;
  transition: background 0.15s;
  flex: none;
}

.switch-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}

.switch input:checked + .switch-track {
  background: var(--accent);
}

.switch input:checked + .switch-track::after {
  transform: translateX(16px);
}

.switch input:focus-visible + .switch-track {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* ---- Settings drawer ------------------------------------------------------------------- */

.drawer {
  margin: 0 0 0 auto;
  height: 100%;
  max-height: 100%;
  width: min(440px, 100%);
  max-width: 100%;
  padding: 0;
  border: 0;
  background: var(--bg);
  color: var(--text);
  box-shadow: var(--shadow);
}

.drawer[open] {
  animation: slide-in 0.2s ease-out;
}

.drawer::backdrop {
  background: rgb(0 0 0 / 0.45);
}

.drawer-inner {
  min-height: 100%;
  padding: calc(8px + var(--safe-top)) calc(14px + var(--safe-right)) calc(20px + var(--safe-bottom)) 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  background: var(--bg);
  padding: 4px 0;
  z-index: 1;
}

.drawer-head h2 {
  font-size: 1.25rem;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel h3 {
  font-size: 1rem;
}

.panel p {
  margin: 0;
}

.row-form {
  display: flex;
  gap: 8px;
}

.row-form input {
  flex: 1;
  min-width: 0;
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}

.list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-top: 1px solid var(--border);
}

.list-item:first-child {
  border-top: 0;
}

.list-main {
  flex: 1;
  min-width: 0;
}

.list-title {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.list-meta {
  font-size: 0.8rem;
}

.list-empty {
  padding: 6px 0;
  font-size: 0.9rem;
}

.badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  vertical-align: 1px;
}

.badge--on {
  background: color-mix(in srgb, var(--ok) 18%, transparent);
  color: var(--ok);
}

.token-box {
  border: 2px solid var(--warn);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.token-box input {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.85rem;
}

.warning {
  color: var(--warn);
  font-weight: 600;
}

/* ---- Toasts ---------------------------------------------------------------------------- */

.toasts {
  position: fixed;
  z-index: 50;
  left: 0;
  right: 0;
  top: calc(64px + var(--safe-top));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  width: 100%;
  max-width: 480px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 10px 10px 14px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 12px;
  box-shadow: var(--shadow);
  animation: toast-in 0.2s ease-out;
}

.toast--error {
  border-left-color: var(--danger);
}

.toast-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.92rem;
  overflow-wrap: anywhere;
}

.toast-close {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.3rem;
  line-height: 1;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  flex: none;
}

/* ---- Wider screens --------------------------------------------------------------------- */

@media (min-width: 720px) {
  .messages-wrap {
    padding-top: 24px;
  }

  .auth-card {
    padding: 36px 32px;
  }

  .toasts {
    align-items: flex-end;
    top: auto;
    bottom: 120px;
    right: 16px;
  }
}

/* ---- Motion ---------------------------------------------------------------------------- */

@keyframes pulse {
  50% {
    opacity: 0.35;
  }
}

@keyframes blink {
  to {
    visibility: hidden;
  }
}

@keyframes bounce {
  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

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

@keyframes ring {
  50% {
    box-shadow: 0 0 0 10px color-mix(in srgb, var(--danger) 15%, transparent);
  }
}

@keyframes slide-in {
  from {
    transform: translateX(100%);
  }
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
}

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

  .msg--assistant.is-streaming .msg-text::after {
    content: none;
  }
}

@media (forced-colors: active) {
  .bubble,
  .toast,
  .panel {
    border: 1px solid CanvasText;
  }
}
