:root {
  --bg: #12141a;
  --bg2: #191c24;
  --line: #2c313d;
  --txt: #eef1f7;
  --dim: #98a1b3;
  --yellow: #f7da21;
  --green: #96c653;
  --blue: #8bbfe6;
  --purple: #bb8bd9;
  --correct: #4a9d5b;
  --wrong: #d0554f;
  --tile: #262b36;
  --tile-on: #3a4152;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--txt);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
}

body { display: flex; justify-content: center; }

#app {
  width: 100%;
  max-width: 1040px;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) clamp(12px, 2vw, 30px) env(safe-area-inset-bottom);
}

/* ---------- top bar ---------- */
#topbar { border-bottom: 1px solid var(--line); }

.tb-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 0;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg2);
  color: var(--dim);
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  flex: 0 0 auto;
}
.icon-btn:hover { color: var(--txt); border-color: #454c5c; }

#brand { text-align: center; flex: 1 1 auto; min-width: 0; }

#brand h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: .16em;
  font-weight: 800;
  line-height: 1.1;
}

#puzzle-label {
  display: block;
  font-size: 11px;
  color: var(--dim);
  letter-spacing: .1em;
  text-transform: uppercase;
  margin-top: 2px;
}

/* ---------- board ---------- */
#board {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 14px 0 8px;
}

#tagline {
  margin: 0 0 10px;
  color: var(--dim);
  font-size: 14px;
  text-align: center;
}

#mistakes {
  font-size: 13px;
  color: var(--dim);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 20px;
}

#chances { display: inline-flex; gap: 5px; }

.x-mark {
  width: 15px;
  height: 15px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 800;
  color: var(--wrong);
  opacity: .35;
  transition: opacity .2s, transform .2s;
}
.x-mark.used { opacity: 1; transform: scale(1.25); }

/* ---------- solved bars ---------- */
#solved {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 7px;
  margin-bottom: 12px;
}

.group-bar {
  border-radius: 7px;
  padding: clamp(11px, 1.6vh, 18px) 8px;
  text-align: center;
  color: #16181d;
  font-weight: 800;
  animation: barIn .34s cubic-bezier(.2,.9,.3,1.2) both;
}

.group-bar .g-name {
  display: block;
  font-size: clamp(17px, 2vw, 26px);
  letter-spacing: .04em;
  text-transform: uppercase;
  line-height: 1.15;
}

.group-bar .g-words {
  display: block;
  font-size: clamp(11px, 1.2vw, 15px);
  font-weight: 600;
  opacity: .78;
  margin-top: 3px;
  letter-spacing: .04em;
}

.group-bar.yellow  { background: var(--yellow); }
.group-bar.green   { background: var(--green); }
.group-bar.blue    { background: var(--blue); }
.group-bar.purple  { background: var(--purple); }

@keyframes barIn {
  from { transform: translateY(-14px) scale(.85); opacity: 0; }
  to   { transform: none; opacity: 1; }
}

/* ---------- grid ---------- */
#grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: clamp(7px, .9vw, 12px);
}

.tile {
  position: relative;
  border: 0;
  border-radius: 7px;
  background: var(--tile);
  color: var(--txt);
  font-size: clamp(12px, 2.2vw, 25px);
  font-weight: 700;
  letter-spacing: .02em;
  padding: clamp(14px, 2vh, 22px) 6px;
  min-height: clamp(58px, 7.5vh, 92px);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  line-height: 1.15;
  transition: background .13s, color .13s, transform .13s, opacity .2s;
  word-break: keep-all;
  overflow-wrap: normal;
}

.tile.s1 { font-size: clamp(10px, 1.85vw, 21px); }
.tile.s2 { font-size: clamp(9px, 1.5vw, 17px); }

.tile:hover { background: #2f3542; }
.tile.on {
  background: var(--tile-on);
  color: #fff;
  transform: translateY(-2px);
  box-shadow: 0 3px 0 rgba(0,0,0,.35);
}
.tile.gone { opacity: 0; transform: scale(.6); pointer-events: none; }

.tile.wrongpop { animation: shake .4s; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(7px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

.tile.wrongsel { background: var(--wrong); color: #fff; }

/* ---------- message ---------- */
#message {
  width: 100%;
  text-align: center;
  border-radius: 7px;
  padding: 11px;
  font-size: 14px;
  font-weight: 700;
  margin-top: 12px;
  animation: msgIn .25s ease both;
}
#message.hidden { display: none; }
#message.bad { background: rgba(208,85,79,.16); color: #ff9b95; border: 1px solid rgba(208,85,79,.5); }
#message.good { background: rgba(74,157,91,.16); color: #8ee0a4; border: 1px solid rgba(74,157,91,.5); }
#message.info { background: var(--bg2); color: var(--dim); border: 1px solid var(--line); }
@keyframes msgIn { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

/* ---------- controls ---------- */
#controls, #postgame {
  display: flex;
  gap: 9px;
  margin-top: 14px;
  width: 100%;
  justify-content: center;
  flex-wrap: wrap;
}
#postgame { flex-direction: column; align-items: stretch; }
#postgame.hidden, #controls.hidden { display: none; }

.ctl {
  border: 1px solid var(--line);
  background: var(--bg2);
  color: var(--txt);
  border-radius: 7px;
  padding: 12px 16px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: filter .15s, opacity .15s;
}
.ctl:hover:not(:disabled) { filter: brightness(1.25); }
.ctl:disabled { opacity: .4; cursor: not-allowed; }
.ctl.primary { background: var(--correct); border-color: var(--correct); color: #fff; }
.ctl.wide { width: 100%; }

/* ---------- footer ---------- */
#foot {
  text-align: center;
  color: #5d6577;
  font-size: 11px;
  padding: 14px 0 16px;
  letter-spacing: .03em;
}

/* ---------- modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 50;
  animation: msgIn .18s ease both;
}
.modal.hidden { display: none; }

.modal-card {
  background: var(--bg2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 22px 20px 20px;
  width: 100%;
  max-width: 420px;
  max-height: 88vh;
  overflow-y: auto;
  position: relative;
}
.modal-card h2 { margin: 0 0 12px; font-size: 19px; }
.modal-card h3 { margin: 18px 0 8px; font-size: 14px; color: var(--dim); text-transform: uppercase; letter-spacing: .08em; }
.modal-card p { font-size: 14px; line-height: 1.55; color: #c8cfdc; margin: 0 0 10px; }
.modal-card ul { margin: 0 0 12px; padding-left: 18px; }
.modal-card li { font-size: 14px; line-height: 1.6; color: #c8cfdc; margin-bottom: 5px; }
.modal-card .tiny { font-size: 12px; color: var(--dim); }

.modal-close {
  position: absolute;
  top: 8px; right: 10px;
  background: none;
  border: 0;
  color: var(--dim);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
}
.modal-close:hover { color: #fff; }

.sw {
  display: inline-block;
  width: 11px; height: 11px;
  border-radius: 3px;
  vertical-align: -1px;
}
.sw.yellow { background: var(--yellow); }
.sw.green { background: var(--green); }
.sw.blue { background: var(--blue); }
.sw.purple { background: var(--purple); }

/* stats */
#stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 6px;
}
.stat {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 4px;
  text-align: center;
}
.stat b { display: block; font-size: 24px; line-height: 1.1; }
.stat span { font-size: 10px; color: var(--dim); text-transform: uppercase; letter-spacing: .05em; }

#dist { display: flex; flex-direction: column; gap: 5px; }
.dist-row { display: flex; align-items: center; gap: 7px; font-size: 12px; }
.dist-row .n { width: 10px; color: var(--dim); text-align: right; }
.dist-row .bar {
  background: #3a4152;
  color: #fff;
  font-weight: 700;
  font-size: 11px;
  padding: 3px 7px;
  border-radius: 3px;
  min-width: 22px;
  text-align: right;
  transition: width .4s;
}
.dist-row .bar.hit { background: var(--correct); }
#streak-line { font-size: 13px; color: var(--dim); margin-top: 12px; text-align: center; }

#share-text {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--txt);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 14px;
  line-height: 1.7;
  padding: 10px;
  resize: none;
  margin-bottom: 10px;
}

/* toast */
#toast {
  position: fixed;
  left: 50%;
  bottom: 34px;
  transform: translateX(-50%);
  background: #f4f6fa;
  color: #14161c;
  font-size: 14px;
  font-weight: 700;
  padding: 11px 18px;
  border-radius: 8px;
  z-index: 60;
  box-shadow: 0 6px 24px rgba(0,0,0,.45);
  animation: msgIn .2s ease both;
}
#toast.hidden { display: none; }

@media (max-width: 380px) {
  .tile { min-height: 50px; padding: 11px 2px; font-size: 12px; }
  #brand h1 { font-size: 17px; }
}

@media (min-width: 640px) {
  .tile { min-height: clamp(66px, 8vh, 96px); font-size: clamp(17px, 2.2vw, 25px); }
  .tile.s1 { font-size: clamp(15px, 1.85vw, 21px); }
  .tile.s2 { font-size: clamp(13px, 1.5vw, 17px); }
  #brand h1 { font-size: 26px; }
}
