/* HanziLoop — styles */
:root[data-theme="dark"] {
  --bg: #121212; --surface: #1E1E1E; --field: #1C1C1C; --text: #F2F2F2; --muted: #A6A6A6;
  --border: rgba(255,255,255,.10); --mapBg: #0B0B0B; --glass: rgba(28,28,28,.72); --soft: #262626;
  --accent: #8CCBA6; --accentFg: #0F2A1C; --tabOn: #3A3A3A;
  --good: #6FD39A; --goodBg: rgba(111,211,154,.14); --bad: #FF8A80; --badBg: rgba(255,138,128,.14);
}
:root[data-theme="light"] {
  --bg: #F7F8F2; --surface: #FFFFFF; --field: #FFFFFF; --text: #25382E; --muted: #5B6B60;
  --border: rgba(37,56,46,.14); --mapBg: #E6EADF; --glass: rgba(255,255,255,.72); --soft: #F2F4EE;
  --accent: #2F5D47; --accentFg: #FFFFFF; --tabOn: #E1EBDD;
  --good: #1E7A4A; --goodBg: rgba(30,122,74,.10); --bad: #C0392B; --badBg: rgba(192,57,43,.10);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: Inter, 'Noto Sans Thai', 'Noto Sans SC', sans-serif;
  background: var(--bg); color: var(--text);
  transition: background .25s, color .25s;
}
button { font-family: inherit; color: inherit; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.zh { font-family: 'Noto Sans SC', sans-serif; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.accent { color: var(--accent); }
.glass {
  background: var(--glass);
  backdrop-filter: blur(20px) saturate(160%); -webkit-backdrop-filter: blur(20px) saturate(160%);
  box-shadow: inset 0 0 0 1px var(--border);
}

.app { height: 100vh; min-height: 560px; display: flex; flex-direction: column; overflow: hidden; }

/* ---------- top bar ---------- */
.top { flex: 0 0 auto; display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; padding: 14px 24px; }
.logo { font-size: 24px; font-weight: 800; letter-spacing: -0.02em; min-width: 150px; }
.search { flex: 1 1 320px; max-width: 580px; position: relative; }
.search-box {
  display: flex; align-items: center; gap: 10px; height: 44px; padding: 0 16px; border-radius: 14px;
  background: var(--field); box-shadow: inset 0 0 0 1px var(--border);
}
.search-box input { flex: 1; min-width: 0; height: 40px; border: 0; background: transparent; font: inherit; font-size: 15px; color: var(--text); }
.search-box input:focus { outline: none; }
.search-box input::placeholder { color: #8A8F8C; }
.drop {
  position: absolute; left: 0; right: 0; top: 52px; z-index: 30; padding: 6px; border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0,0,0,.25), inset 0 0 0 1px var(--border); max-height: 420px; overflow-y: auto;
}
.res { width: 100%; display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 6px 10px; border: 0; border-radius: 10px; background: transparent; cursor: pointer; text-align: left; }
.res:hover, .res.on { background: rgba(127,127,127,.16); }
.res .zh { font-size: 22px; min-width: 64px; }
.res .mid { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.res .py { font-size: 13px; font-weight: 600; color: var(--accent); }
.res .mean { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.drop-note { padding: 6px 10px 4px; font-size: 11px; color: var(--muted); }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.fav-count { font-size: 12px; color: var(--muted); }

.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 12px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  background: var(--field); box-shadow: inset 0 0 0 1px var(--border);
}
.icon-btn.small { width: 32px; height: 32px; border-radius: 8px; background: transparent; box-shadow: none; color: var(--muted); }
.tag { font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 6px; white-space: nowrap; }
.btn { min-height: 44px; padding: 0 16px; border: 1px solid var(--border); border-radius: 12px; background: transparent; font-size: 13px; font-weight: 600; cursor: pointer; }
.btn.primary { border: 0; background: var(--accent); color: var(--accentFg); }

/* ---------- main / map ---------- */
.main { flex: 1 1 auto; min-height: 0; position: relative; }
.view { position: absolute; top: 0; bottom: 0; left: 24px; right: 24px; }
.map-wrap { border-radius: 18px; overflow: hidden; background: var(--mapBg); }
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; transition: filter .3s ease; }
#cv.pointing { cursor: pointer; }
#cv.dragging { cursor: grabbing; }
.map-wrap.blurred #cv { filter: blur(3px) brightness(.8); }
.chip { position: absolute; left: 14px; top: 14px; display: flex; gap: 8px; padding: 6px 12px; border-radius: 999px; font-size: 12px; pointer-events: none; }
.chip b { font-weight: 600; }
.zoom { position: absolute; left: 14px; bottom: 14px; display: flex; flex-direction: column; gap: 6px; }

.hover-card {
  position: absolute; z-index: 5; pointer-events: none; width: 260px; padding: 14px 16px; border-radius: 18px;
  box-shadow: 0 18px 50px rgba(0,0,0,.32), inset 0 0 0 1px var(--border);
  display: flex; flex-direction: column; gap: 6px;
  opacity: 0; transform: translateY(8px) scale(.95); transform-origin: 0 50%; visibility: hidden;
  transition: opacity .22s ease, transform .22s cubic-bezier(.2,.8,.2,1), left .16s ease, top .16s ease, visibility 0s linear .22s;
}
.hover-card.show { opacity: 1; transform: none; visibility: visible; transition: opacity .22s ease, transform .22s cubic-bezier(.2,.8,.2,1), left .16s ease, top .16s ease, visibility 0s; }
.hover-card .head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.hover-card .zh { font-size: 38px; font-weight: 500; line-height: 1.1; }
.hover-card .py { font-size: 16px; font-weight: 600; color: var(--accent); }
.hover-card .en, .hover-card .th { font-size: 13px; line-height: 1.45; }
.hover-card .th { color: var(--accent); }
.hover-card .pos { font-size: 11px; color: var(--muted); }

.backdrop { position: fixed; inset: 0; z-index: 40; display: none; }
.backdrop.on { display: block; }
.detail {
  position: absolute; z-index: 50; right: 16px; top: 16px; width: 300px; max-width: calc(100% - 32px); max-height: calc(100% - 32px);
  overflow-y: auto; padding: 20px; border-radius: 22px; background: var(--surface); box-shadow: 0 20px 60px rgba(0,0,0,.35);
  display: flex; flex-direction: column; gap: 14px;
  opacity: 0; transform: translateY(10px) scale(.95); transform-origin: 100% 0; pointer-events: none; visibility: hidden;
  transition: opacity .26s ease, transform .26s cubic-bezier(.2,.8,.2,1), visibility 0s linear .26s;
}
.detail.open { opacity: 1; transform: none; pointer-events: auto; visibility: visible; transition: opacity .26s ease, transform .26s cubic-bezier(.2,.8,.2,1), visibility 0s; }
.detail .bar { display: flex; justify-content: space-between; align-items: center; }
.detail .big { font-size: 64px; font-weight: 500; line-height: 1.1; text-align: center; }
.detail .info { text-align: center; display: flex; flex-direction: column; gap: 5px; }
.detail .info .py { font-size: 20px; font-weight: 600; color: var(--accent); }
.detail .info .en, .detail .info .th { font-size: 15px; }
.detail .info .th { color: var(--accent); }
.detail .ex { border-radius: 12px; padding: 12px; background: var(--soft); display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.detail .ex b { font-size: 11px; }
.detail .acts { display: flex; gap: 8px; }
.detail .acts .btn { flex: 1; min-height: 46px; padding: 0 8px; }
.fav-btn { flex: 1.3 !important; border: 0 !important; display: flex; align-items: center; justify-content: center; gap: 6px; background: var(--accent); color: var(--accentFg); }
.fav-btn.on { background: #FFC93C; color: #3A2A00; }

/* ---------- flashcards ---------- */
.cards-view { overflow-y: auto; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; padding: 16px 0; }
.empty { max-width: 420px; text-align: center; display: flex; flex-direction: column; gap: 10px; align-items: center; }
.empty b { font-size: 18px; }
.empty p { margin: 0; font-size: 14px; color: var(--muted); line-height: 1.6; }
.fcard {
  width: 460px; max-width: 100%; min-height: 300px; border: 0; border-radius: 26px; cursor: pointer; padding: 28px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: var(--surface); box-shadow: 0 18px 50px rgba(0,0,0,.18);
  animation: hlPop .3s cubic-bezier(.2,.8,.2,1);
}
.fcard .front { font-size: 96px; font-weight: 500; line-height: 1.1; }
.fcard .bzh { font-size: 40px; font-weight: 500; }
.fcard .bpy { font-size: 24px; font-weight: 600; color: var(--accent); }
.fcard .ben, .fcard .bth { font-size: 17px; text-align: center; line-height: 1.5; }
.fcard .bth { color: var(--accent); }
.fc-acts { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }

/* ---------- quiz ---------- */
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.quiz-view { overflow-y: auto; display: flex; flex-direction: column; align-items: center; padding: 8px 0 16px; }
.qz { width: 100%; max-width: 620px; margin: auto 0; display: flex; flex-direction: column; gap: 16px; }
.qz h1 { margin: 0; font-size: 26px; font-weight: 800; letter-spacing: -0.01em; }
.qz .sub { font-size: 14px; color: var(--muted); line-height: 1.6; }
.qz-panel { padding: 18px; border-radius: 18px; background: var(--surface); box-shadow: inset 0 0 0 1px var(--border); display: flex; flex-direction: column; gap: 18px; }
.qz-panel b { font-size: 13px; }
.qz-row { display: flex; flex-wrap: wrap; gap: 8px; }
.pill { min-height: 40px; padding: 0 14px; border-radius: 10px; cursor: pointer; font-size: 13px; font-weight: 600; border: 1px solid var(--border); background: transparent; }
.pill[aria-pressed="true"] { background: var(--accent); color: var(--accentFg); border-color: transparent; }
.mode { min-height: 52px; padding: 8px 14px; border-radius: 12px; cursor: pointer; display: flex; align-items: center; gap: 12px; text-align: left; border: 1px solid var(--border); background: transparent; width: 100%; }
.mode .box { flex: 0 0 auto; width: 22px; height: 22px; border-radius: 6px; display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 800; box-shadow: inset 0 0 0 1.5px var(--border); color: var(--accentFg); }
.mode[aria-pressed="true"] { border-color: var(--accent); background: var(--soft); }
.mode[aria-pressed="true"] .box { background: var(--accent); box-shadow: none; }
.mode .t { display: flex; flex-direction: column; gap: 2px; }
.mode .t span { font-size: 12px; color: var(--muted); }
.btn.big { min-height: 48px; padding: 0 22px; font-size: 15px; border-radius: 14px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.bar-prog { height: 8px; border-radius: 999px; background: var(--soft); overflow: hidden; }
.bar-prog i { display: block; height: 100%; border-radius: 999px; background: var(--accent); transition: width .35s ease; }
.qcard { padding: 28px 24px; border-radius: 26px; background: var(--surface); box-shadow: 0 18px 50px rgba(0,0,0,.18); display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center; animation: hlPop .3s cubic-bezier(.2,.8,.2,1); }
.qcard .hz { font-size: 84px; font-weight: 500; line-height: 1.1; }
.qcard .pt { font-size: 26px; font-weight: 700; line-height: 1.35; }
.qcard .rv { font-size: 14px; color: var(--accent); }
.choices { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.choice { min-height: 68px; padding: 10px 14px; border-radius: 14px; cursor: pointer; text-align: left; display: flex; flex-direction: column; justify-content: center; gap: 2px; border: 1.5px solid var(--border); background: var(--surface); transition: background .2s, border-color .2s, opacity .2s, transform .12s; }
.choice:hover:not(:disabled) { transform: translateY(-1px); border-color: var(--accent); }
.choice:disabled { cursor: default; }
.choice .l { font-size: 15px; font-weight: 600; line-height: 1.35; }
.choice .l.zh { font-size: 28px; font-weight: 400; line-height: 1.2; }
.choice .s { font-size: 12px; opacity: .75; line-height: 1.35; }
.choice.good { background: var(--goodBg); border-color: var(--good); color: var(--good); }
.choice.bad { background: var(--badBg); border-color: var(--bad); color: var(--bad); }
.choice.dim { opacity: .55; }
.fb { text-align: center; font-size: 15px; font-weight: 700; min-height: 22px; }
.fb.good { color: var(--good); } .fb.bad { color: var(--bad); }
.center-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.score { font-size: 56px; font-weight: 800; letter-spacing: -0.02em; color: var(--accent); }
.wrong-row { display: flex; align-items: baseline; gap: 12px; padding: 8px 0; border-top: 1px solid var(--border); }

/* ---------- table ---------- */
.table-view { overflow-y: auto; border-radius: 18px; background: var(--surface); box-shadow: inset 0 0 0 1px var(--border); padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.table-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.filters button { min-height: 36px; padding: 0 12px; border-radius: 10px; cursor: pointer; font-size: 12px; font-weight: 600; border: 1px solid var(--border); background: transparent; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; min-width: 720px; }
th { text-align: left; font-size: 12px; color: var(--muted); padding: 8px; border-bottom: 1px solid var(--border); }
td { padding: 8px; border-bottom: 1px solid var(--border); }
tbody tr:hover { background: rgba(127,127,127,.08); }
td.zh { font-size: 18px; white-space: nowrap; }
td.py { white-space: nowrap; color: var(--accent); }
.star-col { width: 44px; }
.star { width: 36px; height: 36px; border: 0; border-radius: 8px; background: transparent; cursor: pointer; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.star.on { color: #E0A800; }
.go { min-height: 34px; padding: 0 10px; border: 1px solid var(--border); border-radius: 8px; background: transparent; font-size: 12px; cursor: pointer; white-space: nowrap; }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.pager .btn { min-height: 40px; }

/* ---------- bottom tabs ---------- */
.tabs-wrap { flex: 0 0 auto; display: flex; justify-content: center; padding: 14px 16px 18px; }
.tabs { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; padding: 5px; border-radius: 16px; background: var(--field); box-shadow: inset 0 0 0 1px var(--border); }
.tab { min-height: 42px; padding: 0 18px; border: 0; border-radius: 12px; cursor: pointer; font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; background: transparent; color: var(--muted); transition: background .2s; }
.tab[aria-current="page"] { background: var(--tabOn); color: var(--text); }
.badge { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px; font-size: 11px; display: inline-flex; align-items: center; justify-content: center; background: var(--accent); color: var(--accentFg); }

@keyframes hlIn { from { opacity: 0; transform: translateY(8px) scale(.95); } to { opacity: 1; transform: none; } }
@keyframes hlPop { from { opacity: 0; transform: translateY(10px) scale(.95); } to { opacity: 1; transform: none; } }

@media (max-width: 640px) {
  .top { padding: 12px 14px; }
  .logo { min-width: 0; font-size: 20px; }
  .view { left: 12px; right: 12px; }
  .fav-count { display: none; }
  .tab { padding: 0 12px; font-size: 13px; }
  .choices { grid-template-columns: 1fr; }
  .qcard .hz { font-size: 64px; }
}

/* =========================================================
   Device layouts — set on <html data-device="phone|tablet|desktop">
   ========================================================= */
.tab .ti { display: none; }
.rows-list { display: flex; flex-direction: column; gap: 8px; }
.word-card { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 4px 12px; padding: 12px; border-radius: 14px; background: var(--soft); }
.word-card .zh { font-size: 26px; grid-row: span 2; }
.word-card .py { color: var(--accent); font-weight: 600; font-size: 14px; }
.word-card .mn { font-size: 13px; color: var(--muted); line-height: 1.4; grid-column: 2; }
.word-card .side { grid-row: span 2; display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.device-chip { display: none; }
button, a, input { touch-action: manipulation; }
.app { height: 100dvh; }

/* ---------- tablet (iPad) ---------- */
[data-device="tablet"] .top { padding: max(12px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) 12px max(20px, env(safe-area-inset-left)); }
[data-device="tablet"] .logo { min-width: 0; }
[data-device="tablet"] .view { left: 20px; right: 20px; }
[data-device="tablet"] .icon-btn { width: 48px; height: 48px; }
[data-device="tablet"] .zoom .icon-btn { width: 52px; height: 52px; }
[data-device="tablet"] .detail { width: 360px; top: 16px; right: 16px; padding: 24px; }
[data-device="tablet"] .detail .big { font-size: 76px; }
[data-device="tablet"] .detail .acts .btn { min-height: 52px; font-size: 15px; }
[data-device="tablet"] .tabs-wrap { padding: 12px 16px max(14px, env(safe-area-inset-bottom)); }
[data-device="tablet"] .tab { min-height: 52px; padding: 0 22px; font-size: 15px; }
[data-device="tablet"] .tab .ti { display: inline-flex; }
[data-device="tablet"] .btn { min-height: 48px; }
[data-device="tablet"] .choice { min-height: 80px; }
[data-device="tablet"] .fcard { width: 560px; min-height: 360px; }
[data-device="tablet"] .fcard .front { font-size: 120px; }
[data-device="tablet"] .qz { max-width: 720px; }
[data-device="tablet"] .star, [data-device="tablet"] .go { min-height: 44px; min-width: 44px; }

/* ---------- phone (iPhone) ---------- */
[data-device="phone"] .top { padding: max(10px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) 10px max(14px, env(safe-area-inset-left)); gap: 10px; }
[data-device="phone"] .logo { min-width: 0; font-size: 20px; }
[data-device="phone"] .search { order: 3; flex: 1 1 100%; max-width: none; }
[data-device="phone"] .search-box input { font-size: 16px; } /* 16px stops iOS from zooming on focus */
[data-device="phone"] .fav-count { display: none; }
[data-device="phone"] .icon-btn { width: 40px; height: 40px; }
[data-device="phone"] .view { left: 10px; right: 10px; }
[data-device="phone"] .map-wrap { border-radius: 14px; }
[data-device="phone"] .zoom { left: auto; right: 10px; bottom: 10px; }
[data-device="phone"] .zoom .icon-btn { width: 40px; height: 40px; }
[data-device="phone"] .chip { left: 10px; top: 10px; }
[data-device="phone"] .hover-card { display: none; }
/* detail becomes a bottom sheet */
[data-device="phone"] .detail { position: fixed; left: 0; right: 0; bottom: 0; top: auto; width: auto; max-width: none; max-height: 78dvh; border-radius: 24px 24px 0 0; padding: 26px 18px calc(18px + env(safe-area-inset-bottom)); transform: translateY(105%); transform-origin: 50% 100%; box-shadow: 0 -10px 40px rgba(0,0,0,.35); }
[data-device="phone"] .detail.open { transform: translateY(var(--sheet-drag, 0px)); }
[data-device="phone"] .detail::before { content: ''; position: absolute; top: 9px; left: 50%; width: 40px; height: 5px; margin-left: -20px; border-radius: 99px; background: var(--border); }
[data-device="phone"] .detail .big { font-size: 56px; }
[data-device="phone"] .detail .acts .btn { min-height: 50px; font-size: 14px; }
/* tab bar: full width, icon above label, like an iOS tab bar */
[data-device="phone"] .tabs-wrap { padding: 0; border-top: 1px solid var(--border); background: var(--glass); backdrop-filter: blur(20px) saturate(160%); -webkit-backdrop-filter: blur(20px) saturate(160%); }
[data-device="phone"] .tabs { width: 100%; flex-wrap: nowrap; justify-content: space-around; gap: 0; padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); border-radius: 0; background: transparent; box-shadow: none; }
[data-device="phone"] .tab { flex: 1; flex-direction: column; gap: 3px; min-height: 52px; padding: 4px 2px; font-size: 10.5px; border-radius: 10px; position: relative; }
[data-device="phone"] .tab .ti { display: inline-flex; }
[data-device="phone"] .tab[aria-current="page"] { background: transparent; color: var(--accent); }
[data-device="phone"] .tab .badge { position: absolute; top: 2px; left: 50%; margin-left: 6px; min-width: 16px; height: 16px; font-size: 10px; padding: 0 4px; }
[data-device="phone"] .main { margin-bottom: 8px; }
[data-device="phone"] .cards-view { padding: 8px 0; gap: 14px; }
[data-device="phone"] .fcard { min-height: 260px; padding: 20px; border-radius: 22px; }
[data-device="phone"] .fcard .front { font-size: 76px; }
[data-device="phone"] .fc-acts .btn { flex: 1 1 calc(50% - 8px); }
[data-device="phone"] .qz { gap: 12px; }
[data-device="phone"] .qz h1 { font-size: 22px; }
[data-device="phone"] .qcard { padding: 20px 16px; border-radius: 22px; }
[data-device="phone"] .qcard .hz { font-size: 64px; }
[data-device="phone"] .qcard .pt { font-size: 21px; }
[data-device="phone"] .choices { grid-template-columns: 1fr; gap: 8px; }
[data-device="phone"] .choice { min-height: 58px; }
[data-device="phone"] .center-row .btn { flex: 1 1 100%; }
[data-device="phone"] .table-view { padding: 12px; border-radius: 14px; }
[data-device="phone"] .filters { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -2px; padding-bottom: 2px; }
[data-device="phone"] .filters::-webkit-scrollbar { display: none; }
[data-device="phone"] .filters button { flex: 0 0 auto; }
[data-device="phone"] #tableScroll { display: none; }
[data-device="phone"] .pager .btn { flex: 1; }
[data-device="phone"] .pager { flex-wrap: wrap; }
[data-device="phone"] .pager > div { display: flex; gap: 6px; flex: 1; }
