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

: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;
  --button-text: #121212;
}

@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);
  color: var(--text);
  background: var(--background);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

main { width: min(100%, 760px); margin: 0 auto; }

a:focus-visible,
button:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }

.page-header,
.title-row,
.section-heading,
.daily-goal-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.back-link { color: var(--text); font-size: .85rem; font-weight: 700; text-decoration: none; }
.back-link:hover { text-decoration: underline; }

.theme-toggle {
  display: inline-flex;
  min-height: 36px;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface);
  font: inherit;
  font-size: .76rem;
  font-weight: 700;
  cursor: pointer;
}
.theme-toggle:hover { border-color: var(--text); color: var(--text); }
.theme-toggle-icon { font-size: 1rem; line-height: 1; }

.title-row { margin: 38px 0 20px; align-items: end; }
.eyebrow, .section-kicker { margin: 0 0 5px; color: var(--green); font-size: .72rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
h1 { margin: 0; font-size: clamp(1.75rem, 5vw, 2.3rem); letter-spacing: -.035em; }
h2 { margin: 0; font-size: 1.05rem; }
.topics-link, .primary-link, .recommendation a {
  display: inline-flex;
  min-height: 40px;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  border: 1px solid var(--text);
  border-radius: 10px;
  color: var(--text);
  background: var(--surface);
  font-size: .82rem;
  font-weight: 750;
  text-decoration: none;
}
.topics-link:hover, .primary-link:hover, .recommendation a:hover { color: var(--button-text); background: var(--text); }

.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; }

.connection-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"] .connection-notice { background: #342e1e; }
.sync-diagnostic { display: block; margin-top: 5px; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; overflow-wrap: anywhere; }

.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; }
.view-state .primary-link, .view-state button { margin-top: 16px; }
.state-mark, .loading-dot { display: grid; width: 36px; height: 36px; flex: none; place-items: center; border-radius: 50%; color: var(--green); background: var(--green-soft); font-weight: 800; }
.loading-dot { width: 14px; height: 14px; margin: 4px 11px; animation: pulse 1s ease-in-out infinite alternate; }
.error-state { border-color: var(--red); }
.error-state .state-mark { color: var(--red); background: var(--red-soft); }
.error-state button { min-height: 38px; padding: 0 13px; border: 1px solid var(--text); border-radius: 9px; color: var(--text); background: var(--surface); font: inherit; font-weight: 700; cursor: pointer; }

#dashboard { display: grid; gap: 34px; }
.recommendation { display: flex; align-items: center; justify-content: space-between; gap: 20px; padding: 20px; border: 1px solid var(--green); border-radius: 14px; background: var(--green-soft); }
.recommendation p:not(.section-kicker) { margin: 6px 0 0; color: var(--muted); font-size: .85rem; }
.recommendation a { flex: none; border-color: var(--green); color: var(--button-text); background: var(--green); }
.section-heading { margin-bottom: 12px; align-items: end; }
.section-heading > span { color: var(--muted); font-size: .75rem; }

.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.kpi { display: grid; gap: 7px; min-width: 0; padding: 15px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.kpi span, .daily-goal-card span { color: var(--muted); font-size: .73rem; }
.kpi strong { font-size: clamp(1.15rem, 3vw, 1.45rem); }
.daily-goal-card { margin-top: 10px; padding: 14px 15px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.daily-goal-card > div:first-child { display: grid; gap: 4px; }
.goal-track { width: min(45%, 260px); height: 8px; overflow: hidden; border-radius: 999px; background: var(--border); }
.goal-track span { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--green); transition: width 180ms ease; }

.level-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.level-card { padding: 15px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.level-card-header { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.level-card h3 { margin: 0; font-size: 1rem; }
.level-card strong { font-size: .8rem; }
.level-card p { margin: 8px 0 10px; color: var(--muted); font-size: .72rem; }
.level-track { height: 5px; overflow: hidden; border-radius: 99px; background: var(--border); }
.level-track span { display: block; height: 100%; background: var(--green); }

.activity-grid { padding: 15px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.activity-calendar-grid { display: grid; grid-template-columns: repeat(14, 1fr); gap: 5px; }
.activity-calendar-day { aspect-ratio: 1; border-radius: 4px; background: var(--border); }
.activity-calendar-day[data-intensity="1"] { background: color-mix(in srgb, var(--green) 35%, var(--surface)); }
.activity-calendar-day[data-intensity="2"] { background: color-mix(in srgb, var(--green) 65%, var(--surface)); }
.activity-calendar-day[data-intensity="3"] { background: color-mix(in srgb, var(--green) 82%, var(--surface)); }
.activity-calendar-day[data-intensity="4"] { background: var(--green); }

.achievement-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.achievement-card { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 12px; padding: 15px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.achievement-card[data-unlocked="false"] { color: var(--muted); }
.achievement-icon { display: grid; width: 38px; height: 38px; place-items: center; border-radius: 50%; color: var(--muted); background: var(--background); font-size: 1.05rem; font-weight: 850; }
.achievement-card[data-unlocked="true"] .achievement-icon { color: var(--green); background: var(--green-soft); }
.achievement-content { display: grid; gap: 5px; min-width: 0; }
.achievement-content h3 { margin: 0; font-size: .88rem; }
.achievement-content p { margin: 0; color: var(--muted); font-size: .73rem; line-height: 1.4; }
.achievement-progress { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 8px; margin-top: 4px; }
.achievement-track { height: 5px; overflow: hidden; border-radius: 99px; background: var(--border); }
.achievement-track span { display: block; height: 100%; border-radius: inherit; background: var(--green); }
.achievement-progress > span { color: var(--muted); font-size: .68rem; }
.achievement-unlocked-at { color: var(--green) !important; font-weight: 700; }

.achievement-toast { position: fixed; z-index: 10; right: max(16px, calc((100vw - 760px) / 2)); bottom: 22px; display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 11px; width: min(calc(100vw - 32px), 390px); padding: 12px 13px; border: 1px solid var(--green); border-radius: 13px; color: var(--text); background: var(--surface); box-shadow: 0 12px 35px rgb(0 0 0 / .18); animation: toast-in 180ms ease-out; }
.achievement-toast-icon { display: grid; width: 34px; height: 34px; place-items: center; border-radius: 50%; color: var(--green); background: var(--green-soft); font-weight: 850; }
.achievement-toast > div { display: grid; gap: 2px; }
.achievement-toast > div span { color: var(--green); font-size: .68rem; font-weight: 800; letter-spacing: .04em; text-transform: uppercase; }
.achievement-toast > div strong { font-size: .85rem; }
.achievement-toast button { display: grid; width: 32px; height: 32px; place-items: center; border: 0; border-radius: 50%; color: var(--muted); background: transparent; font: inherit; font-size: 1.2rem; cursor: pointer; }
.achievement-toast button:hover { color: var(--text); background: var(--background); }

.list-card, .history-list { margin: 0; padding: 0; overflow: hidden; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); }
.weak-topic, .history-item { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 14px; padding: 14px 16px; }
.weak-topic + .weak-topic, .history-item + .history-item { border-top: 1px solid var(--border); }
.weak-topic a { overflow: hidden; color: var(--text); font-size: .86rem; font-weight: 700; text-decoration: none; text-overflow: ellipsis; white-space: nowrap; }
.weak-topic a:hover { text-decoration: underline; }
.weak-topic span, .history-meta, .list-empty { color: var(--muted); font-size: .75rem; }
.history-list { list-style: none; }
.history-main { display: grid; gap: 4px; min-width: 0; }
.history-main strong { overflow: hidden; font-size: .84rem; text-overflow: ellipsis; white-space: nowrap; }
.history-score { font-size: .82rem; font-weight: 800; }
.list-empty { margin: 0; padding: 18px 16px; }

[hidden] { display: none !important; }

@keyframes pulse { from { opacity: .35; transform: scale(.8); } to { opacity: 1; transform: scale(1); } }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

@media (prefers-reduced-motion: reduce) {
  .loading-dot { animation: none; }
  .goal-track span { transition: none; }
  .achievement-toast { animation: none; }
}

@media (max-width: 620px) {
  .kpi-grid, .level-grid { grid-template-columns: repeat(2, 1fr); }
  .achievement-grid { grid-template-columns: 1fr; }
  .activity-calendar-grid { grid-template-columns: repeat(7, 1fr); }
  .recommendation { align-items: flex-start; flex-direction: column; }
  .recommendation a { width: 100%; }
}

@media (max-width: 390px) {
  .title-row { align-items: flex-start; flex-direction: column; }
  .topics-link { align-self: stretch; }
  .daily-goal-card { align-items: stretch; flex-direction: column; }
  .goal-track { width: 100%; }
  .achievement-toast { right: 16px; bottom: 16px; }
}
