/* Three visual palettes, with system mode resolved before the first paint. */
:root {
  color-scheme: light;
  font-family: system-ui, -apple-system, sans-serif;
  --bg: #f6f7fb; --panel: #fff; --ink: #1d2431; --muted: #4d596b;
  --line: #cbd4e1; --tint: #e5efff; --accent: #204da2; --accent-ink: #fff;
  --shadow: 0 14px 38px #263c681b; --backdrop: #141e3d85;
  background: var(--bg); color: var(--ink);
}
:root[data-appearance="dark"] {
  color-scheme: dark;
  --bg: #151a24; --panel: #202738; --ink: #eef1f8; --muted: #b3bcd0;
  --line: #505b74; --tint: #273d62; --accent: #a8c6ff; --accent-ink: #132a52;
  --shadow: 0 16px 44px #080a1099; --backdrop: #0b101bc9;
}
:root[data-appearance="contrast"] {
  color-scheme: light;
  --bg: #fff; --panel: #fff; --ink: #090909; --muted: #232323;
  --line: #292d37; --tint: #e5efff; --accent: #002e83; --accent-ink: #fff;
  --shadow: none; --backdrop: #060812bf;
}
* { box-sizing: border-box }
[hidden] { display: none !important }
body { margin: 0; min-height: 100dvh }
button, input, textarea { color: inherit; font: inherit }
button, .button { min-height: 44px; cursor: pointer; }
button:disabled, .file-input:disabled + label, label:has(+ input:disabled) { opacity: .55; cursor: default }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px }
h1, h2, p { margin: 0 }
img { display: inline-block; border-radius: 16px }
.button, .primary {
  display: inline-flex; align-items: center; justify-content: center; gap: .35em; padding: .55rem .85rem;
  border: 1px solid var(--line); border-radius: 13px; background: var(--panel); color: var(--ink); white-space: nowrap;
}
.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 650 }
.loading, .login { min-height: 100dvh; display: grid; place-items: center; padding: 1rem }
#login-form {
  width: min(100%, 26rem); display: grid; gap: .7rem; padding: clamp(1rem, 6vw, 2rem);
  border: 1px solid var(--line); border-radius: 25px; background: var(--panel); box-shadow: var(--shadow);
}
#login-form h1 { margin-bottom: .65rem; font-size: 1.55rem }
#login-form input { min-height: 50px; width: 100%; padding: .55rem .75rem; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); font-size: 1.1rem }
#login-form .primary { margin-top: .3rem }
.topbar {
  flex: none; z-index: 2; display: flex; align-items: center; justify-content: space-between; gap: .4rem;
  padding: max(.7rem, env(safe-area-inset-top)) max(.8rem, env(safe-area-inset-right)) .7rem max(.8rem, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line); background: var(--bg);
}
.topbar h1 { font-size: clamp(.95rem, 4vw, 1.2rem); text-align: center }
#app { display: flex; flex-direction: column; height: 100vh; height: var(--chat-height, 100dvh); overflow: hidden }
.main { flex: 1 1 0; min-height: 0; width: 100%; overflow-y: auto; overscroll-behavior-y: contain; overflow-anchor: none; padding: clamp(1rem, 4vw, 2.3rem) }
.main > section { width: min(100%, 50rem); margin-inline: auto }
.main:focus-visible { outline-offset: -4px }
#welcome { display: grid; place-items: center; gap: .9rem; margin: max(8vh, 1rem) auto; text-align: center }
#welcome h2 { font-size: clamp(1.6rem, 6vw, 2.5rem); letter-spacing: -.03em }
#messages { display: flex; flex-direction: column; gap: 1.25rem }
.message { font-size: clamp(1.08rem, 4vw, 1.2rem); line-height: 1.65; max-width: 100%; overflow-wrap: anywhere }
.message.mine { align-self: flex-end; max-width: 91%; border-radius: 18px 18px 4px 18px; background: var(--tint); padding: .65rem .95rem }
.message .body { white-space: pre-wrap }
.message .actions { margin-top: .4rem; display: flex; gap: .4rem }
.message .actions .button { font-size: .92rem; color: var(--muted); }
.composer { flex: none; background: var(--bg); padding: .5rem max(.85rem, env(safe-area-inset-right)) max(.5rem, env(safe-area-inset-bottom)) max(.85rem, env(safe-area-inset-left)); }
#chat-form {
  margin: auto; width: min(100%, 50rem); border: 1px solid var(--line); border-radius: 22px; background: var(--panel);
  box-shadow: var(--shadow); padding: .55rem;
}
#message { display: block; width: 100%; min-height: 42px; max-height: 28dvh; resize: none; border: 0; background: transparent; padding: .5rem .65rem; font-size: 18px; line-height: 1.4 }
.composer-actions { display: flex; gap: .45rem; align-items: center }
#send { margin-left: auto }
.file-input { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%) }
.attach:has(+ input:focus-visible) { outline: 3px solid var(--accent); outline-offset: 3px }
#attachment { display: flex; align-items: center; gap: .45rem; min-width: 0; padding: .1rem .45rem .35rem; color: var(--muted) }
#attachment-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap }
#status, #login-error, #account-status { color: var(--muted); font-size: .93rem; line-height: 1.4; }
#status:not(:empty) { padding: .3rem .65rem }
#status:empty, #login-error:empty, #account-status:empty { display: none }
#history-dialog { margin: 0 auto 0 0; width: min(90vw, 365px); max-width: none; height: 100dvh; max-height: none; border: 0; border-right: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: max(1rem, env(safe-area-inset-top)) 1rem max(1rem, env(safe-area-inset-bottom)); }
#history-dialog[open] { display: flex; flex-direction: column; gap: .75rem }
#history-dialog::backdrop { background: var(--backdrop) }
.history-head { display: flex; align-items: center; justify-content: space-between; gap: .45rem; }
.history-head h2 { font-size: 1.25rem }
#history-list { display: grid; align-content: start; gap: .4rem; flex: 1; overflow: auto; }
.history-item { text-align: left; padding: .6rem .7rem; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; overflow-wrap: anywhere }
.history-item[aria-current="true"] { background: var(--tint) }
.logout { align-self: stretch }
.history-search, .history-note { color: var(--muted); font-size: .92rem }
#history-search { width: 100%; min-height: 44px; padding: .5rem .7rem; border: 1px solid var(--line); border-radius: 11px; background: var(--bg); font-size: 16px }
.history-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .35rem; align-items: start }
.history-row > .history-item { min-width: 0 }
.delete-chat { color: var(--muted); font-size: .9rem }
.history-setting { display: flex; align-items: center; gap: .45rem; min-height: 44px }
.history-setting input { accent-color: var(--accent); width: 1.15rem; height: 1.15rem }
#stop { margin-left: auto; color: var(--accent); font-weight: 650 }
.streaming { white-space: pre-wrap }
.message:not(.mine) .body { white-space: normal }
.message .body :is(h1,h2,h3,h4,h5,h6,p,pre,blockquote,ul,ol,table) { margin: .55em 0 }
.message .body h1 { font-size: 1.4em }
.message .body h2 { font-size: 1.25em }
.message .body :is(h3,h4,h5,h6) { font-size: 1.1em }
.message .body :is(ul,ol) { padding-left: 1.4em }
.message .body blockquote { border-left: 3px solid var(--line); padding-left: .8em; color: var(--muted) }
.message .body pre { padding: .7em; overflow-x: auto; background: var(--tint); border-radius: 12px; white-space: pre }
.message .body code { font-family: ui-monospace, monospace; font-size: .9em }
.message .body a { color: var(--accent); text-underline-offset: .18em }
.message .body table { display: block; overflow-x: auto; border-collapse: collapse }
.message .body :is(td, th) { border: 1px solid var(--line); padding: .35em .55em }
.action-dialog { width: min(92vw, 30rem); margin: auto; max-height: 90dvh; border: 1px solid var(--line); border-radius: 20px; background: var(--panel); color: var(--ink); padding: 1.15rem }
.action-dialog::backdrop { background: var(--backdrop) }
.action-dialog form { display: grid; gap: .65rem }
.action-dialog h2 { font-size: 1.25rem }
.action-dialog :is(input, textarea) { min-height: 45px; width: 100%; padding: .6rem; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; font-size: 16px }
.dialog-actions { display: flex; align-items: center; justify-content: flex-end; gap: .45rem }
#rename-error:empty { display: none }
.history-actions summary { list-style: none; font-size: .90rem }
.history-actions summary::-webkit-details-marker { display: none }
.history-actions > div { display: grid; gap: .25rem; padding-top: .25rem }
.history-actions button { justify-content: flex-start; font-size: .90rem }
.pin-label { display: inline-block; margin-right: .4rem; font-size: .75rem; color: var(--accent); font-weight: 650 }
.mine .actions .button { min-height: 44px; font-size: .82rem; border-color: transparent; background: transparent; padding: 0 .25rem }
.app-heading { display: grid; justify-items: center; gap: .12rem }
.version { color: var(--muted); font-size: .72rem; font-weight: 600; font-variant-numeric: tabular-nums }
.usage-dialog { max-height: 86dvh; overflow: auto }
.usage-dialog .history-note { margin-top: .6rem }
#usage-summary { margin: .8rem 0; font-weight: 650 }
.usage-row { border-top: 1px solid var(--line); padding: .6rem .1rem; }
.usage-row h3 { margin: 0 .2rem .15rem 0; font-size: 1rem; overflow-wrap: anywhere }
.usage-row p, .usage-row time { color: var(--muted); font-size: .88rem }

.personal-dialog { max-height: 86dvh; overflow: auto; }
.personal-section { display: grid; gap: .6rem; margin-top: .8rem; padding: .8rem 0; border-top: 1px solid var(--line); }
.personal-section h3 { font-size: 1.02rem; margin: 0; }
#theme-choice { min-height: 44px; width: 100%; color: inherit; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: .45rem .7rem; font: inherit; }
.memory-head { display: flex; justify-content: space-between; align-items: baseline; gap: .4rem; }
.memory-head span, #personal-status { color: var(--muted); font-size: .92rem; }
.memory-row { padding: .65rem .7rem; margin: .45rem 0; border: 1px solid var(--line); border-radius: 12px; overflow-wrap: anywhere; }
.memory-row p { white-space: pre-wrap; line-height: 1.5; }
.memory-actions { display: flex; gap: .4rem; margin-top: .35rem; }
.memory-actions .button { min-height: 44px; font-size: .92rem; }
#memory-add { justify-self: start; }
#personal-status:empty { display: none; }
