:root {
  color-scheme: light;
  --bg: #f2f4f0;
  --surface: #ffffff;
  --surface-soft: #f7f8f5;
  --ink: #17221d;
  --muted: #65716b;
  --line: #dfe4de;
  --line-strong: #cbd3cb;
  --brand: #175f46;
  --brand-soft: #e1f0e8;
  --amber: #a8641d;
  --amber-soft: #fff0da;
  --red: #aa443b;
  --red-soft: #f9e5e2;
  --row-height: 80px;
  --columns: 58px minmax(160px, 0.9fr) minmax(230px, 1.4fr) 132px 300px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background: var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, select { font: inherit; }
button { color: inherit; }

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

.topbar {
  height: 68px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 0 max(22px, calc((100vw - 1280px) / 2));
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 11px;
  color: #fff;
  background: var(--brand);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.35rem;
  font-weight: 700;
}

.brand-copy { display: grid; gap: 1px; min-width: 0; }
.brand-copy strong { font-size: 0.98rem; letter-spacing: -0.015em; }
.brand-copy span { color: var(--muted); font-size: 0.73rem; }

.topbar-actions { display: flex; align-items: center; gap: 10px; }

.session-button {
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 11px;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  color: #4e5d55;
  background: var(--surface);
  cursor: pointer;
  font-size: 0.73rem;
  font-weight: 750;
}

.session-button:hover { background: var(--surface-soft); }
.session-button:focus-visible { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(23, 95, 70, 0.14); }
.session-button svg { width: 16px; flex: 0 0 auto; }
.session-button[data-state="synced"] { border-color: #a9cbbd; color: var(--brand); background: var(--brand-soft); }
.session-button[data-state="syncing"] svg { animation: sync-pulse 900ms ease-in-out infinite alternate; }
.session-button[data-state="error"] { border-color: #dba49e; color: var(--red); background: var(--red-soft); }

@keyframes sync-pulse { to { opacity: 0.35; } }

.progress-summary {
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress-stat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface-soft);
  font-size: 0.75rem;
}

.progress-stat strong { color: var(--ink); font-variant-numeric: tabular-nums; }
.progress-stat em { font-style: normal; }
.progress-dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.progress-stat.known { color: var(--brand); }
.progress-stat.familiar { color: var(--amber); }
.progress-stat.unknown { color: var(--red); }
.progress-stat.unmarked { color: var(--muted); }

.workspace {
  width: min(1280px, 100%);
  min-height: 0;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-self: center;
  gap: 12px;
  padding: 18px 22px 20px;
}

.toolbar {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) 190px 190px 165px;
  gap: 10px;
}

.control {
  position: relative;
  min-width: 0;
}

.control-icon {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 14px;
  width: 18px;
  color: var(--muted);
  transform: translateY(-50%);
  pointer-events: none;
}

.control input,
.control select,
.sort-button {
  width: 100%;
  height: 44px;
  border: 1px solid var(--line-strong);
  border-radius: 11px;
  outline: none;
  color: var(--ink);
  background: var(--surface);
  transition: border-color 140ms ease, box-shadow 140ms ease, background 140ms ease;
}

.control input { padding: 0 14px 0 42px; }
.control select { padding: 0 36px 0 13px; cursor: pointer; }
.control input::placeholder { color: #8b958f; }
.control input:focus,
.control select:focus,
.sort-button:focus-visible,
.knowledge-button:focus-visible {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(23, 95, 70, 0.14);
}

.sort-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 13px;
  cursor: pointer;
  font-size: 0.81rem;
  font-weight: 700;
}

.sort-button:hover { background: var(--surface-soft); }
.sort-button svg { width: 17px; color: var(--brand); }

.list-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 20px;
  padding: 0 3px;
  color: var(--muted);
  font-size: 0.76rem;
}

.list-summary strong { color: var(--ink); font-weight: 750; }

.table-shell {
  min-height: 0;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 8px 30px rgba(33, 50, 41, 0.06);
}

.table-head {
  height: 38px;
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: var(--columns);
  align-items: center;
  border-bottom: 1px solid var(--line);
  color: #7a857e;
  background: var(--surface-soft);
  font-size: 0.65rem;
  font-weight: 800;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}

.table-head span { padding: 0 13px; }
.table-head span:first-child { text-align: right; }

.list-viewport {
  position: relative;
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-anchor: none;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: #b5beb7 transparent;
  contain: layout paint;
}

.list-viewport::-webkit-scrollbar { width: 10px; }
.list-viewport::-webkit-scrollbar-thumb {
  border: 3px solid transparent;
  border-radius: 999px;
  background: #b5beb7;
  background-clip: padding-box;
}

.list-inner { position: relative; width: 100%; }

.word-row {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: var(--row-height);
  display: grid;
  grid-template-columns: var(--columns);
  align-items: center;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  contain: strict;
}

.word-row:hover { background: #fbfcfa; }
.cell { min-width: 0; padding: 10px 13px; }

.rank {
  color: #8a958e;
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.word {
  overflow: hidden;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.22rem;
  font-weight: 650;
  letter-spacing: -0.015em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.translation {
  display: -webkit-box;
  overflow: hidden;
  color: #3f4b44;
  font-size: 0.82rem;
  line-height: 1.35;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.translation div { display: inline; }
.translation div + div::before { content: " · "; color: #98a19b; }

.meta { display: flex; flex-wrap: wrap; gap: 4px; }

.tag {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  padding: 3px 7px;
  border-radius: 999px;
  color: #486057;
  background: #edf2ee;
  font-size: 0.63rem;
  font-weight: 700;
  line-height: 1;
}

.tag.gender { color: #7a4b20; background: #f6ecdf; }

.knowledge {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  padding-right: 12px;
}

.knowledge-button {
  min-width: 0;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 0 7px;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  outline: none;
  color: #59665f;
  background: #fff;
  cursor: pointer;
  font-size: 0.69rem;
  font-weight: 750;
  white-space: nowrap;
  transition: border-color 120ms ease, color 120ms ease, background 120ms ease;
}

.knowledge-button:hover { border-color: #9aa69f; background: var(--surface-soft); }
.status-icon { font-size: 0.82rem; font-weight: 900; }
.knowledge-button[data-status="known"] .status-icon { color: var(--brand); }
.knowledge-button[data-status="familiar"] .status-icon { color: var(--amber); }
.knowledge-button[data-status="unknown"] .status-icon { color: var(--red); }
.knowledge-button[data-status="known"][aria-pressed="true"] { border-color: #9fc5b5; color: var(--brand); background: var(--brand-soft); }
.knowledge-button[data-status="familiar"][aria-pressed="true"] { border-color: #dfb77e; color: #865018; background: var(--amber-soft); }
.knowledge-button[data-status="unknown"][aria-pressed="true"] { border-color: #dba49e; color: #913a32; background: var(--red-soft); }

.empty {
  height: 100%;
  display: grid;
  place-content: center;
  gap: 5px;
  padding: 32px;
  color: var(--muted);
  text-align: center;
}

.empty strong { color: var(--ink); font-size: 1rem; }

.session-dialog {
  width: min(430px, calc(100% - 28px));
  padding: 28px;
  border: 1px solid var(--line-strong);
  border-radius: 18px;
  color: var(--ink);
  background: var(--surface);
  box-shadow: 0 24px 80px rgba(20, 35, 27, 0.22);
}

.session-dialog::backdrop { background: rgba(21, 32, 27, 0.46); backdrop-filter: blur(3px); }
.session-dialog h2 { margin: 7px 0 8px; font-family: Georgia, "Times New Roman", serif; font-size: 1.75rem; letter-spacing: -0.03em; }
.session-dialog p { margin: 0; color: var(--muted); font-size: 0.88rem; line-height: 1.55; }
.dialog-kicker { color: var(--brand); font-size: 0.68rem; font-weight: 850; letter-spacing: 0.12em; text-transform: uppercase; }
.dialog-close { position: absolute; top: 12px; right: 12px; width: 34px; height: 34px; border: 0; border-radius: 50%; color: var(--muted); background: transparent; cursor: pointer; font-size: 1.4rem; line-height: 1; }
.dialog-close:hover { color: var(--ink); background: var(--surface-soft); }

#sessionForm { display: grid; gap: 11px; margin-top: 20px; }
.secret-field { display: grid; gap: 7px; color: #405048; font-size: 0.76rem; font-weight: 750; }
.secret-field input { width: 100%; height: 46px; padding: 0 13px; border: 1px solid var(--line-strong); border-radius: 10px; outline: none; color: var(--ink); background: #fff; font-size: 16px; }
.secret-field input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(23, 95, 70, 0.14); }
.primary-button, .secondary-button { min-height: 44px; border-radius: 10px; cursor: pointer; font-weight: 800; }
.primary-button { border: 1px solid var(--brand); color: #fff; background: var(--brand); }
.primary-button:hover { background: #104d38; }
.primary-button:disabled { cursor: wait; opacity: 0.65; }
.secondary-button { width: 100%; margin-top: 20px; border: 1px solid var(--line-strong); color: var(--red); background: #fff; }
.secondary-button:hover { background: var(--red-soft); }
.security-note { margin-top: 14px !important; padding: 11px 12px; border-radius: 9px; background: var(--surface-soft); font-size: 0.74rem !important; }
.session-message { margin-top: 12px !important; color: var(--red) !important; }
.session-message:empty { display: none; }

@media (max-width: 900px) {
  :root { --columns: 54px minmax(145px, 0.8fr) minmax(190px, 1.15fr) 105px 266px; }
  .workspace { padding-right: 14px; padding-left: 14px; }
  .toolbar { grid-template-columns: minmax(190px, 1fr) 170px 170px 150px; }
  .knowledge-button { font-size: 0.64rem; }
}

@media (max-width: 800px) {
  :root { --row-height: 188px; }

  .topbar {
    height: 58px;
    gap: 8px;
    padding-right: max(12px, env(safe-area-inset-right));
    padding-left: max(12px, env(safe-area-inset-left));
  }
  .brand { gap: 9px; }
  .brand-mark { width: 34px; height: 34px; border-radius: 10px; font-size: 1.15rem; }
  .brand-copy span { display: none; }
  .topbar-actions { gap: 5px; }
  .session-button { width: 34px; padding: 0; }
  .session-label { display: none; }
  .progress-summary { gap: 4px; }
  .progress-stat { min-width: 30px; min-height: 30px; justify-content: center; gap: 4px; padding: 0 4px; }
  .progress-stat em { display: none; }

  .workspace {
    gap: 9px;
    padding: 10px max(9px, env(safe-area-inset-right)) max(9px, env(safe-area-inset-bottom)) max(9px, env(safe-area-inset-left));
  }
  .toolbar { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 7px; }
  .toolbar .search-control { grid-column: 1 / -1; }
  .toolbar .sort-button { grid-column: 1 / -1; }
  .control input, .control select, .sort-button { height: 44px; border-radius: 10px; }
  .control input, .control select { font-size: 16px; }
  .sort-button { font-size: 0.75rem; }
  .list-summary { font-size: 0.72rem; }
  .table-shell { border-radius: 12px; }
  .table-head { display: none; }

  .word-row {
    grid-template-columns: 42px minmax(0, 1fr);
    grid-template-rows: auto auto auto 48px;
    align-content: center;
    padding: 8px 7px 8px 0;
  }

  .cell { padding: 4px 9px; }
  .rank { grid-row: 1 / 5; align-self: start; padding-top: 8px; }
  .word, .translation, .meta, .knowledge { grid-column: 2; }
  .word { font-size: 1.2rem; }
  .translation { min-height: 22px; font-size: 0.8rem; -webkit-line-clamp: 2; }
  .meta { padding-top: 5px; padding-bottom: 5px; }
  .knowledge { align-self: end; gap: 5px; padding: 3px 8px 0; }
  .knowledge-button { height: 44px; padding: 0 5px; font-size: 0.7rem; }
  .session-dialog { padding: 24px 20px 20px; border-radius: 16px; }
}

@media (max-width: 390px) {
  .progress-summary { gap: 3px; }
  .progress-stat { min-width: 28px; gap: 3px; padding: 0 3px; }
  .status-label { font-size: 0.64rem; }
  .knowledge-button { gap: 3px; }
}

@media (max-width: 360px) {
  .topbar { gap: 5px; }
  .brand { gap: 6px; }
  .brand-mark { width: 30px; height: 30px; }
  .brand-copy strong { font-size: 0.88rem; }
  .topbar-actions { gap: 3px; }
  .session-button { width: 30px; }
  .progress-stat { min-width: 25px; padding: 0 2px; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; }
}
