:root {
  /* плоская тёмно-зелёная палитра: без градиентов, без блюра */
  --bg: #0e1211;
  --bg-soft: #151a18;
  --surface: #1a201d;
  --surface-hi: #232b27;
  --line: #2d3733;
  --text: #e9efec;
  --muted: #98a7a0;
  --primary: #2f9e63;       /* заливка кнопок: чёрный текст даёт 5.5:1 */
  --primary-soft: #5ecb90;  /* зелёный для текста/иконок на тёмном: 8.2:1 */
  --accent: #7ee2a8;
  --ink: #08150e;           /* текст на зелёной заливке */
  --good: #4ade80;
  --bad: #f87171;
  --warn: #d9a52e;
  --radius: 18px;
  --tap: cubic-bezier(0.32, 0.72, 0, 1);
  --tab-inner: 56px;        /* высота полосы вкладок без нижнего безопасного отступа */
  /* нижний отступ под home-индикатор; JS уточняет его (см. syncBottomInset) */
  --sa-bottom: env(safe-area-inset-bottom);
  --kb: 0px;                /* высота клавиатуры iOS (ставится из JS) */
  --column: 760px;          /* ширина колонки на ноутбуке */
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard",
    "Noto Sans KR", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

button { -webkit-user-select: none; user-select: none; }

/* видимый фокус для клавиатуры (ноутбук) */
:focus-visible {
  outline: 2px solid var(--primary-soft);
  outline-offset: 2px;
}
input:focus-visible, textarea:focus-visible { outline: none; }

/* наведение — только там, где есть настоящий курсор */
@media (hover: hover) and (pointer: fine) {
  .primary:hover { filter: brightness(1.07); }
  .ghost:hover { border-color: var(--primary-soft); color: var(--primary-soft); }
  .tab:hover { color: var(--text); }
  .cite:hover { border-color: var(--primary-soft); }
  .lecture:hover { color: var(--primary-soft); }
}

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

/* ---------- gate ---------- */

.gate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(env(safe-area-inset-top) + 24px) 24px calc(env(safe-area-inset-bottom) + 24px);
  background: var(--bg);
}

.gate-card {
  width: 100%;
  max-width: 340px;
  text-align: center;
}

.gate-flower { display: flex; justify-content: center; }
.gate-flower .mark { width: 64px; height: 64px; }
.gate-card h1 { margin: 12px 0 2px; font-size: 30px; letter-spacing: 0.06em; }
.gate-sub { margin: 16px 0 26px; color: var(--muted); font-size: 14px; }

input[type="password"], input[type="text"], textarea {
  width: 100%;
  padding: 15px 16px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-size: 17px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s var(--tap);
}
input:focus, textarea:focus { border-color: var(--primary-soft); }
textarea { resize: none; line-height: 1.6; }

button { font-family: inherit; }

.primary {
  margin-top: 14px;
  width: 100%;
  padding: 15px;
  border: 0;
  border-radius: 14px;
  background: var(--primary);
  color: var(--ink);
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.15s var(--tap), opacity 0.2s, filter 0.2s;
}
.primary:active { transform: scale(0.97); }
.primary:disabled { opacity: 0.5; }
.primary, .ghost { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.error { color: var(--bad); font-size: 14px; margin-top: 12px; }

/* ---------- shell ---------- */

.shell { display: flex; flex-direction: column; height: 100%; height: 100dvh; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  padding-top: env(safe-area-inset-top);
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 18px;
}
.brand { display: flex; align-items: center; }
.mark { display: block; width: 22px; height: 22px; color: var(--primary-soft); }
.note { color: var(--muted); font-size: 13px; white-space: nowrap; }

.topnav { display: none; }

.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 18px 18px calc(var(--tab-inner) + var(--sa-bottom) + 14px);
}
/* карточки не сжимаются, тянуться может только .grow (форма ответа) */
.content > * { flex-shrink: 0; }

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  height: calc(var(--tab-inner) + var(--sa-bottom));
  padding: 0 6px var(--sa-bottom);
  background: var(--bg);
  border-top: 1px solid var(--line);
  transition: transform 0.2s var(--tap);
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 20px;
  line-height: 1.25;
  cursor: pointer;
  border-radius: 12px;
  transition: color 0.2s, background 0.2s;
}
.tab span { font-size: 11px; font-weight: 600; }
.tab.active { color: var(--primary-soft); }

/* клавиатура открыта: composer поднимается над ней, вкладки уезжают */
body.keyboard .tabbar { transform: translateY(120%); }
body.keyboard .composer { bottom: var(--kb); }

/* ---------- cards ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}
.card h2 { margin: 0 0 10px; font-size: 15px; color: var(--muted); font-weight: 600; }

.hero {
  background: #17251e;
  border: 1px solid #2b3d33;
}
.hero h2 { color: #8fe3b4; }
.hero .big { font-size: 26px; font-weight: 700; margin: 0 0 4px; }

.stat-row { display: flex; flex-wrap: wrap; gap: 10px; }
.stat {
  flex: 1;
  min-width: 68px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 13px 10px;
  text-align: center;
}
.stat .num { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .lbl { font-size: 11px; color: var(--muted); margin-top: 2px; word-break: keep-all; }

/* на узких экранах (iPhone SE 1st gen, 320px) — сетка 2×2, а не 4 колонки */
@media (max-width: 380px) {
  .stat { flex: 1 1 calc(50% - 5px); }
}

.chip {
  display: inline-block;
  padding: 7px 11px;
  margin: 0 6px 6px 0;
  border-radius: 999px;
  background: var(--surface-hi);
  border: 1px solid var(--line);
  font-size: 13px;
}
.chip.due { border-color: var(--warn); color: var(--text); }

.actions { display: flex; gap: 10px; margin-bottom: 14px; }
/* обе кнопки делят строку поровну: иначе .primary с width:100% сжимает соседа
   до одной буквы в строке (корейский текст переносится в любом месте) */
.actions > * { flex: 1 1 0; min-width: 0; }
.actions .primary { width: auto; margin-top: 0; }
.ghost {
  flex: 1;
  min-width: 0;
  padding: 15px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s;
}
.ghost:active { transform: scale(0.97); }

/* ---------- quiz ---------- */

.question {
  font-size: 18px;
  font-weight: 600;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: keep-all;
  overflow-wrap: anywhere;
}
.q-label {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.08em;
  margin-bottom: 8px;
}

.score-big { font-size: 40px; font-weight: 800; line-height: 1.1; }
.score-big small { font-size: 16px; color: var(--muted); font-weight: 600; }

.card.grow { flex: 1 1 auto; display: flex; flex-direction: column; }
/* карточка-приглашение («새 문제 받기») стоит по центру свободной высоты */
.card.mid { margin-top: auto; margin-bottom: auto; }
.card.grow textarea { flex: 1 1 auto; min-height: 120px; }

.list { margin: 8px 0 0; padding: 0; list-style: none; }
.list li {
  position: relative;
  padding: 6px 0 6px 22px;
  font-size: 15px;
  word-break: keep-all;
  overflow-wrap: anywhere;
}
.list li::before { position: absolute; left: 2px; }
.list.good li::before { content: "✅"; }
.list.bad li::before { content: "❌"; }
.list.plain li::before { content: "•"; color: var(--muted); }

.answer-text { white-space: pre-wrap; word-break: keep-all; overflow-wrap: anywhere; font-size: 15.5px; }

/* ---------- citations ---------- */

.cites { margin-top: 14px; white-space: normal; }
.cites h2 { margin: 0 0 8px; font-size: 15px; color: var(--muted); font-weight: 600; }
.cite {
  display: block;
  padding: 11px 13px;
  margin-bottom: 8px;
  border-radius: 14px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  text-decoration: none;
  color: var(--text);
  white-space: normal;
  transition: border-color 0.2s;
}
.cite:active { background: var(--surface-hi); }
.cite .cite-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  font-weight: 700;
  color: var(--primary-soft);
  margin-bottom: 4px;
}
.cite .cite-head > span:first-child { min-width: 0; overflow-wrap: anywhere; }
.cite .cite-head > span:last-child { flex: none; white-space: nowrap; color: var(--muted); }
.cite .cite-snippet { font-size: 12.5px; color: var(--muted); line-height: 1.5; overflow-wrap: anywhere; }

/* ---------- chat ---------- */

.bubble {
  padding: 13px 15px;
  border-radius: 16px;
  margin-bottom: 10px;
  font-size: 15.5px;
  word-break: keep-all;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.bubble.user {
  background: #234b38;
  margin-left: 40px;
}
.bubble.bot { background: var(--surface); border: 1px solid var(--line); margin-right: 24px; }

.composer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tab-inner) + var(--sa-bottom));
  z-index: 9;
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.composer textarea { flex: 1; max-height: 120px; }
.composer button {
  width: 52px;
  border: 0;
  border-radius: 14px;
  background: var(--primary);
  color: var(--ink);
  font-size: 20px;
  cursor: pointer;
}
.composer button:disabled { opacity: 0.45; }
.chat-spacer { height: 82px; }

/* ---------- misc ---------- */

.loading {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 14px;
  padding: 14px 2px;
}
.spinner {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid var(--line);
  border-top-color: var(--primary-soft);
  animation: spin 0.8s linear infinite;
  flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

.muted { color: var(--muted); font-size: 13.5px; }
.center { text-align: center; }
.mt { margin-top: 14px; }

.lecture {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 11px 0;
  border-bottom: 1px solid var(--line);
  text-decoration: none;
  color: var(--text);
  font-size: 14.5px;
}
.lecture:last-child { border-bottom: 0; }
.lecture .idx {
  flex: none;
  font-weight: 700;
  color: var(--primary-soft);
  font-variant-numeric: tabular-nums;
}
.lecture .mins { flex: none; color: var(--muted); font-size: 12.5px; }
.lecture .title { flex: 1; min-width: 0; word-break: keep-all; overflow-wrap: anywhere; }

.fade-in { animation: fade 0.28s var(--tap); }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ================= планшет / ноутбук ================= */
/* Уже 768px — мобильная раскладка (нижние вкладки). Шире — колонка 760px
   по центру, вкладки переезжают в верхнюю панель, composer — плавающий. */

@media (min-width: 768px) {
  .topnav { display: flex; gap: 2px; }
  .topnav .tab {
    flex: none;
    flex-direction: row;
    gap: 7px;
    padding: 8px 14px;
    font-size: 17px;
    border-radius: 12px;
  }
  .topnav .tab span { font-size: 14px; }
  .topnav .tab.active { background: var(--surface-hi); }

  .tabbar { display: none; }

  .content {
    width: 100%;
    max-width: var(--column);
    margin: 0 auto;
    padding: 24px 24px 16px;
  }
  .topbar-inner { width: 100%; max-width: calc(var(--column) + 48px); margin: 0 auto; padding: 12px 24px; }

  .card { padding: 20px; border-radius: 20px; }
  .hero .big { font-size: 30px; }
  .question { font-size: 19px; }
  .bubble { font-size: 16px; }
  .bubble.user { margin-left: 18%; }

  .chat-spacer { flex: 1 1 auto; min-height: 16px; height: auto; }
  .composer {
    position: sticky;
    left: auto;
    right: auto;
    bottom: 16px;
    border: 1px solid var(--line);
    border-radius: 18px;
    padding: 12px 14px;
  }
}

@media (min-width: 1400px) {
  :root { --column: 820px; }
}
