:root {
  --background: #f5f5f5;
  --surface: #ffffff;
  --text: #171717;
  --muted: #737373;
  --border: #dedede;
  --green: #16784b;
  --green-soft: #e7f5ed;
  --red: #bd3434;
  --red-soft: #fbeaea;
  --focus: #6aa9ff;
}

:root[data-theme="dark"] {
  --background: #121212;
  --surface: #1d1d1d;
  --text: #f2f2f2;
  --muted: #aaaaaa;
  --border: #393939;
  --green: #74c69d;
  --green-soft: #20372b;
  --red: #ff8b8b;
  --red-soft: #482525;
  --focus: #8bb9ff;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --background: #121212;
    --surface: #1d1d1d;
    --text: #f2f2f2;
    --muted: #aaaaaa;
    --border: #393939;
  }
}

* { box-sizing: border-box; }
html { min-width: 320px; scrollbar-gutter: stable; }
body {
  min-height: 100svh;
  margin: 0;
  padding: clamp(24px, 6vw, 56px) clamp(16px, 5vw, 32px) 92px;
  color: var(--text);
  background: var(--background);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
main { width: min(100%, 680px); margin: 0 auto; }
a:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }

.page-header { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.back-link, .progress-link { color: var(--text); font-size: .84rem; font-weight: 700; text-decoration: none; }
.back-link:hover, .progress-link:hover { text-decoration: underline; }
.title-block { margin: 38px 0 24px; }
.title-block h1 { margin: 0; font-size: clamp(1.75rem, 5vw, 2.25rem); letter-spacing: -.035em; }
.title-block > p:last-child { margin: 8px 0 0; color: var(--muted); font-size: .9rem; }
.eyebrow, .section-kicker { margin: 0 0 5px; color: var(--green); font-size: .72rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
h2 { margin: 0; font-size: 1.08rem; }

.offline-notice { margin-bottom: 14px; padding: 10px 13px; border: 1px solid #d3a93a; border-radius: 10px; color: var(--text); background: #fff7dc; font-size: .8rem; }
:root[data-theme="dark"] .offline-notice { background: #342e1e; }
.auth-panel { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 5px 12px; margin-bottom: 14px; padding: 11px 13px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.google-sign-in { display: inline-flex; width: fit-content; min-height: 38px; align-items: center; gap: 9px; padding: 0 13px; border: 1px solid var(--border); border-radius: 9px; color: var(--text); background: var(--surface); font: inherit; font-size: .84rem; font-weight: 700; cursor: pointer; }
.google-mark { display: grid; width: 20px; height: 20px; place-items: center; border-radius: 50%; color: #fff; background: #4285f4; font-size: .72rem; font-weight: 800; }
.auth-user { display: flex; min-width: 0; align-items: center; gap: 8px; }
.auth-avatar { width: 30px; height: 30px; flex: none; border-radius: 50%; }
.auth-name { overflow: hidden; font-size: .84rem; font-weight: 750; text-overflow: ellipsis; white-space: nowrap; }
.auth-statistics, .auth-status { color: var(--muted); font-size: .72rem; }
.auth-statistics { grid-column: 1; }
.auth-status { grid-column: 1 / -1; }
.auth-sign-out { grid-column: 2; grid-row: 1; min-height: 32px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px; color: var(--muted); background: transparent; font: inherit; font-size: .75rem; font-weight: 700; cursor: pointer; }
.view-state { display: flex; align-items: flex-start; gap: 15px; padding: 24px; border: 1px solid var(--border); border-radius: 14px; background: var(--surface); }
.view-state p { margin: 6px 0 0; color: var(--muted); font-size: .88rem; line-height: 1.5; }
.loading-dot, .state-mark { display: grid; width: 16px; height: 16px; flex: none; margin: 3px 10px; place-items: center; border-radius: 50%; color: var(--green); background: var(--green); animation: pulse 1s ease-in-out infinite alternate; }
.state-mark { width: 36px; height: 36px; margin: 0; color: var(--red); background: var(--red-soft); animation: none; }
.error-state { border-color: var(--red); }
.error-state button { min-height: 38px; margin-top: 16px; padding: 0 13px; border: 1px solid var(--text); border-radius: 9px; color: var(--text); background: var(--surface); font: inherit; font-weight: 700; cursor: pointer; }

#settings-form { display: grid; gap: 28px; }
.settings-section { overflow: hidden; border: 1px solid var(--border); border-radius: 14px; background: var(--surface); }
.section-heading { padding: 18px 20px 15px; border-bottom: 1px solid var(--border); }
.section-heading > p { margin: 7px 0 0; color: var(--muted); font-size: .78rem; }
.segmented-control { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 0; padding: 16px 20px 20px; border: 0; }
.segmented-control label { cursor: pointer; }
.segmented-control input { position: absolute; opacity: 0; pointer-events: none; }
.segmented-control span { display: grid; min-height: 42px; place-items: center; border: 1px solid var(--border); border-radius: 10px; color: var(--muted); font-size: .82rem; font-weight: 750; }
.segmented-control input:checked + span { border-color: var(--green); color: var(--green); background: var(--green-soft); }
.segmented-control input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 3px; }

.field-list { padding: 0 20px; }
.select-field, .switch-field, .text-field { display: grid; grid-template-columns: minmax(0, 1fr) minmax(150px, 220px); align-items: center; gap: 18px; padding: 17px 0; }
.select-field + .select-field, .select-field + .switch-field, .switch-field + .text-field { border-top: 1px solid var(--border); }
.select-field > span, .switch-field > span, .text-field > span { display: grid; gap: 4px; }
.select-field strong, .switch-field strong, .text-field strong { font-size: .85rem; }
.select-field small, .switch-field small, .text-field small { color: var(--muted); font-size: .73rem; line-height: 1.4; }
select, input[type="text"], input[type="search"] { width: 100%; min-height: 40px; padding: 0 11px; border: 1px solid var(--border); border-radius: 9px; color: var(--text); background: var(--surface); font: inherit; font-size: .82rem; }
.switch-field input { width: 42px; height: 23px; justify-self: end; margin: 0; accent-color: var(--green); cursor: pointer; }
.field-help { margin: -10px 0 16px; color: var(--muted); font-size: .7rem; text-align: right; }

.search-field { display: block; padding: 16px 20px 10px; }
.favorite-topics { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; max-height: 360px; margin: 0; padding: 6px 20px 20px; overflow-y: auto; border: 0; }
.favorite-topic { display: flex; min-width: 0; align-items: flex-start; gap: 9px; padding: 10px; border: 1px solid var(--border); border-radius: 9px; cursor: pointer; }
.favorite-topic:hover { border-color: var(--text); }
.favorite-topic input { width: 17px; height: 17px; flex: none; margin: 1px 0 0; accent-color: var(--green); }
.favorite-topic span { overflow: hidden; font-size: .78rem; font-weight: 650; line-height: 1.35; text-overflow: ellipsis; }
.favorite-topic[hidden] { display: none; }
.topics-empty { margin: 0; padding: 0 20px 20px; color: var(--muted); font-size: .78rem; }

.save-status { position: fixed; right: max(16px, calc((100vw - 680px) / 2)); bottom: 22px; display: inline-flex; min-height: 42px; align-items: center; gap: 8px; padding: 0 14px; border: 1px solid var(--green); border-radius: 999px; color: var(--green); background: var(--green-soft); box-shadow: 0 8px 25px rgb(0 0 0 / .12); font-size: .78rem; font-weight: 750; }
.save-status[data-state="saving"] span:first-child { animation: pulse 600ms ease-in-out infinite alternate; }
.save-status[data-state="error"] { border-color: var(--red); color: var(--red); background: var(--red-soft); }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
[hidden] { display: none !important; }
@keyframes pulse { from { opacity: .35; transform: scale(.8); } to { opacity: 1; transform: scale(1); } }
@media (prefers-reduced-motion: reduce) { .loading-dot, .save-status[data-state="saving"] span:first-child { animation: none; } }

@media (max-width: 560px) {
  .select-field, .switch-field, .text-field { grid-template-columns: 1fr; gap: 10px; }
  .switch-field { grid-template-columns: minmax(0, 1fr) auto; }
  .switch-field input { grid-column: 2; grid-row: 1; }
  .field-help { text-align: left; }
  .favorite-topics { grid-template-columns: 1fr; }
  .save-status { right: 16px; left: 16px; justify-content: center; }
}

@media (max-width: 350px) {
  .page-header { align-items: flex-start; flex-direction: column; }
  .segmented-control { grid-template-columns: 1fr; }
}
