:root {
  --green: #6aaa64; --yellow: #c9b458; --gray: #787c7e;
  --bg: #fff; --fg: #1a1a1b; --fg2: #787c7e; --border: #d3d6da; --border-filled: #878a8c;
  --key: #d3d6da; --key-fg: #1a1a1b; --toast-bg: #1a1a1b; --toast-fg: #fff; --modal: #fff; --shadow: rgba(0,0,0,.2);
  --tile: 62px; --key-h: 58px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --green: #538d4e; --yellow: #b59f3b; --gray: #3a3a3c;
  --bg: #121213; --fg: #d7dadc; --fg2: #818384; --border: #3a3a3c; --border-filled: #565758;
  --key: #818384; --key-fg: #d7dadc; --toast-bg: #d7dadc; --toast-fg: #121213; --modal: #121213; --shadow: rgba(0,0,0,.6);
  color-scheme: dark;
}
:root[data-contrast="1"] { --green: #f5793a; --yellow: #85c0f9; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { margin: 0; height: 100%; }
body { margin: 0; min-height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  display: flex; flex-direction: column; align-items: center;
  -webkit-tap-highlight-color: transparent;
}
button { font-family: inherit; cursor: pointer; }

/* ---- header ---- */
header {
  width: 100%; max-width: 500px; height: 51px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  border-bottom: 1px solid var(--border); padding: 0 8px;
}
header .title {
  grid-column: 2; font-family: Georgia, "Times New Roman", serif; font-weight: 700; font-size: 28px;
  letter-spacing: .02em; text-transform: uppercase; white-space: nowrap;
}
header .title small { font-size: 14px; letter-spacing: .15em; color: var(--fg2); margin-left: 6px; font-family: inherit; }
header .right { grid-column: 3; display: flex; justify-content: flex-end; gap: 4px; align-items: center; }
header .left { grid-column: 1; font-size: 13px; color: var(--fg2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { background: none; border: 0; color: var(--fg); font-size: 20px; padding: 4px 6px; border-radius: 4px; }
.icon-btn:hover { background: var(--border); }

/* ---- board ---- */
#game { flex: 1 0 auto; min-height: 0; width: 100%; max-width: 500px; display: flex; flex-direction: column; }
#board-wrap { flex: 1 0 auto; min-height: 360px; display: flex; padding: 8px 0; }
#board { display: grid; grid-auto-rows: var(--tile); gap: 5px; margin: auto; }
.row { display: grid; grid-template-columns: repeat(5, var(--tile)); gap: 5px; }
.tile {
  width: var(--tile); height: var(--tile); border: 2px solid var(--border); display: inline-flex; justify-content: center; align-items: center;
  font-size: 2rem; line-height: 1; font-weight: 700; text-transform: uppercase; user-select: none; color: var(--fg);
}
.tile[data-state="tbd"] { border-color: var(--border-filled); animation: pop 100ms; }
.tile[data-state="correct"], .tile[data-state="present"], .tile[data-state="absent"] { border-color: transparent; color: #fff; }
.tile[data-state="correct"] { background: var(--green); }
.tile[data-state="present"] { background: var(--yellow); }
.tile[data-state="absent"] { background: var(--gray); }
.tile.flip { animation: flip 500ms ease forwards; }
.tile.bounce { animation: bounce 1000ms ease; }
.row.shake { animation: shake 600ms; }

@keyframes pop { from { transform: scale(.8); opacity: 0; } 40% { transform: scale(1.1); opacity: 1; } }
@keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(-90deg); } 100% { transform: rotateX(0); } }
@keyframes bounce {
  0%, 20% { transform: translateY(0); } 40% { transform: translateY(-30px); } 50% { transform: translateY(5px); }
  60% { transform: translateY(-15px); } 80% { transform: translateY(2px); } 100% { transform: translateY(0); }
}
@keyframes shake {
  10%, 90% { transform: translateX(-1px); } 20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); } 40%, 60% { transform: translateX(4px); }
}

/* ---- intro / counter / mật thư 2 ---- */
.intro { width: 100%; max-width: 500px; border-bottom: 1px solid var(--border); flex: none; }
.intro summary { cursor: pointer; padding: 10px 14px; list-style: none; display: flex; align-items: center; gap: 8px; position: sticky; top: 0; background: var(--bg); }
.intro summary::-webkit-details-marker { display: none; }
.intro summary::before { content: '▾'; color: var(--fg2); transition: transform .15s; }
.intro:not([open]) summary::before { transform: rotate(-90deg); }
.intro-title { font-family: Georgia, serif; font-weight: 700; font-size: 18px; text-transform: uppercase; letter-spacing: .06em; }
.intro-body { padding: 0 14px 12px; font-size: 14px; line-height: 1.45; }
.intro-body p { margin: 8px 0; }
.intro ul { margin: 8px 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; }
.intro .rules { list-style: disc; padding-left: 20px; gap: 4px; }
.intro .legend li { display: flex; gap: 10px; align-items: center; }
.intro .legend .tile { --tile: 34px; flex: none; font-size: 1.2rem; }
.counter { text-align: center; font-size: 13px; font-weight: 700; padding: 6px 8px; color: var(--fg2); border-bottom: 1px solid var(--border); flex: none; }
.s2 { flex: 1; min-height: 0; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 10px; border-top: 1px solid var(--border); }
.s2 h2 { margin: 0; font-family: Georgia, serif; text-transform: uppercase; letter-spacing: .06em; font-size: 18px; text-align: center; }
.s2 .clue { white-space: pre-wrap; border: 2px solid var(--border-filled); border-radius: 8px; padding: 12px; font-size: 15px; line-height: 1.5; }
.s2 .muted-note { text-align: center; color: var(--fg2); margin: 0; }
.s2 .ok-note { text-align: center; color: var(--green); font-weight: 700; margin: 0; }
.s2-form { display: flex; flex-wrap: wrap; gap: 8px; }
.s2-form input { flex: 1; min-width: 160px; font: inherit; padding: 10px; border: 2px solid var(--border); border-radius: 4px; background: transparent; color: var(--fg); }
.s2-form .err { flex-basis: 100%; }
.big-btn { display: block; text-align: center; text-decoration: none; font-size: 22px; padding: 16px 28px; }
.link-text { text-align: center; word-break: break-all; user-select: all; font-size: 13px; color: var(--fg2); margin: 0; }
#game.s2mode #board-wrap { flex: 0 1 auto; min-height: 0; max-height: 130px; overflow-y: auto; }

/* ---- keyboard ---- */
#keyboard { height: 200px; margin: 0 8px 8px; user-select: none; }
#keyboard .krow { display: flex; width: 100%; margin: 0 auto 8px; touch-action: manipulation; }
.key {
  flex: 1; height: var(--key-h); margin: 0 6px 0 0; border: 0; border-radius: 4px; background: var(--key); color: var(--key-fg);
  font-weight: 700; font-size: 1.25em; text-transform: uppercase; display: flex; justify-content: center; align-items: center;
}
.key:last-of-type { margin: 0; }
.key.wide { flex: 1.5; font-size: 12px; }
.spacer { flex: .5; }
.key[data-state="correct"] { background: var(--green); color: #fff; }
.key[data-state="present"] { background: var(--yellow); color: #fff; }
.key[data-state="absent"] { background: var(--gray); color: #fff; }

/* ---- toast ---- */
#toasts { position: fixed; top: 60px; left: 50%; transform: translateX(-50%); z-index: 50; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.toast { background: var(--toast-bg); color: var(--toast-fg); font-weight: 700; font-size: 14px; padding: 13px; border-radius: 4px; opacity: 1; transition: opacity 300ms; }
.toast.fade { opacity: 0; }

/* ---- modal / cards ---- */
.overlay { position: fixed; inset: 0; background: var(--shadow); display: flex; justify-content: center; align-items: center; z-index: 100; }
.overlay[hidden] { display: none; }
.card { background: var(--modal); color: var(--fg); border-radius: 8px; box-shadow: 0 4px 23px var(--shadow); padding: 24px; width: min(90vw, 420px); position: relative; }
.card h2 { font-size: 16px; letter-spacing: .05em; text-transform: uppercase; text-align: center; margin: 0 0 16px; }
.card .big { font-size: 40px; font-weight: 300; text-align: center; margin: 8px 0; }
.card p { text-align: center; margin: 8px 0; }
.card .close { position: absolute; top: 12px; right: 12px; }
.answer { display: inline-flex; gap: 4px; }
.answer span { width: 32px; height: 32px; display: inline-flex; justify-content: center; align-items: center; background: var(--green); color: #fff; font-weight: 700; border-radius: 3px; }

/* ---- login ---- */
#login { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 24px; width: 100%; max-width: 500px; }
#login h1 { font-family: Georgia, serif; font-size: 34px; margin: 0; }
#login p { color: var(--fg2); text-align: center; margin: 0; }
#login input, .codes input, .codes textarea {
  width: 100%; font: inherit; padding: 12px; border: 2px solid var(--border); border-radius: 4px; background: transparent; color: var(--fg);
}
#login input { font-size: 24px; text-align: center; letter-spacing: .3em; text-transform: uppercase; font-weight: 700; }
.btn { background: var(--green); color: #fff; border: 0; border-radius: 999px; padding: 12px 28px; font-weight: 700; font-size: 16px; }
.btn.secondary { background: var(--key); color: var(--key-fg); }
.btn.small { padding: 5px 12px; font-size: 13px; }
.btn.danger { background: #c0392b; }
.err { color: #c0392b; font-weight: 700; min-height: 1.2em; margin: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

@media (max-width: 420px) { :root { --tile: 56px; } #keyboard { height: 180px; } .key { font-size: 1em; } }
@media (max-height: 640px) { :root { --tile: 48px; --key-h: 48px; } #keyboard { height: 170px; } }

/* ---- host dashboard ---- */
body.host { align-items: stretch; }
body.host header { max-width: none; }
#dash { padding: 12px 16px 40px; display: flex; flex-direction: column; gap: 14px; }
#dash[hidden], #host-login[hidden] { display: none; }
#host-login { max-width: 420px; margin: 60px auto; display: flex; flex-direction: column; gap: 12px; padding: 24px; }
#host-login input { width: 100%; font: inherit; font-size: 18px; padding: 12px; border: 2px solid var(--border); border-radius: 4px; background: transparent; color: var(--fg); }
.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.stat { border: 1px solid var(--border); border-radius: 8px; padding: 10px 14px; }
.stat b { display: block; font-size: 28px; font-weight: 300; }
.stat span { font-size: 12px; color: var(--fg2); text-transform: uppercase; letter-spacing: .05em; }
.dist { display: grid; grid-template-columns: 24px 1fr; gap: 4px 8px; align-items: center; font-size: 13px; max-width: 420px; }
.dist .bar { height: 20px; background: var(--gray); color: #fff; font-weight: 700; display: flex; justify-content: flex-end; align-items: center; padding: 0 6px; min-width: 24px; box-sizing: border-box; }
.dist .bar.best { background: var(--green); }
.dist .bar.fail { background: #c0392b; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.toolbar select { font: inherit; padding: 6px; border: 1px solid var(--border); border-radius: 4px; background: var(--bg); color: var(--fg); }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--fg2); cursor: pointer; user-select: none; white-space: nowrap; }
th.sorted::after { content: ' ▾'; }
tr.placeholder td { color: var(--fg2); font-style: italic; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--fg2); margin-right: 6px; vertical-align: middle; }
.dot.on { background: var(--green); }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.badge.idle { background: var(--border); color: var(--fg); }
.badge.playing { background: var(--yellow); color: #fff; }
.badge.solved { background: var(--green); color: #fff; }
.badge.failed { background: #c0392b; color: #fff; }
.mini { display: grid; grid-auto-rows: 16px; gap: 2px; max-height: 120px; overflow-y: auto; }
.mini .row { grid-template-columns: repeat(5, 16px); gap: 2px; }
.mini .tile { width: 16px; height: 16px; font-size: 10px; border-width: 1px; }
.mini .tile[data-state="correct"], .mini .tile[data-state="present"], .mini .tile[data-state="absent"] { border-color: transparent; }
.mono { font-family: ui-monospace, Menlo, monospace; }
.muted { color: var(--fg2); font-size: 12px; }
.codes { border: 1px solid var(--border); border-radius: 8px; padding: 14px; display: none; flex-direction: column; gap: 10px; }
.codes.open { display: flex; }
.codes textarea { min-height: 120px; font-family: ui-monospace, Menlo, monospace; }
.codes .inline { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.codes .inline input { width: auto; flex: 1; min-width: 120px; padding: 8px; }
.codes ul { margin: 0; padding-left: 18px; color: #c0392b; font-size: 13px; }
td .name-edit { font: inherit; background: transparent; color: var(--fg); border: 1px dashed transparent; border-radius: 3px; padding: 1px 4px; width: 130px; }
td .name-edit:hover, td .name-edit:focus { border-color: var(--border-filled); }

.fld { display: flex; flex-direction: column; gap: 4px; font-size: 13px; flex: 1; min-width: 160px; }
.fld textarea, .fld input { width: 100%; font: inherit; padding: 8px; border: 2px solid var(--border); border-radius: 4px; background: transparent; color: var(--fg); }
.fld textarea { min-height: 90px; }
#settings .err { margin: 0; }
.badge.wordle { background: var(--gray); color: #fff; }
.badge.clue2 { background: var(--yellow); color: #fff; }
.badge.done { background: var(--green); color: #fff; }
.s2 .penalty-note { margin: 0; text-align: center; font-weight: 700; color: var(--yellow); line-height: 1.4; }
.s2 .post-img { display: block; max-width: 100%; height: auto; margin: 0 auto; border-radius: 6px; }
.s2 .embed { display: flex; justify-content: center; min-height: 60px; }
.s2 .embed blockquote a { display: block; padding: 16px; color: var(--fg2); }
.s2 > * { flex-shrink: 0; }
.s2 .embed-fallback { color: var(--fg2); text-decoration: underline; padding: 8px; }
