:root {
  --bg:        #111318;
  --bg-soft:   #171a21;
  --bg-card:   #1c2029;
  --line:      #262b36;
  --fg:        #e6e9ef;
  --fg-dim:    #8b93a3;
  --accent:    #5b9dff;
  --ready:     #3ddc84;
  --hold:      #e0b341;
  --danger:    #ff6b6b;

  --u: #f2f2f2;
  --d: #f5d020;
  --f: #22b14c;
  --b: #2a6fdb;
  --l: #ff8a1e;
  --r: #e03434;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#app {
  height: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 14px calc(10px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  gap: 10px;
}

/* ---------- scramble bar ---------- */

#topbar {
  position: relative;      /* the ↻ button sits inside the scramble box's right gutter */
  display: flex;
  padding-top: 12px;
  --gutter: 52px;
}

#scramble {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: clamp(18px, 4.6vw, 34px);
  line-height: 1.4;
  letter-spacing: .04em;
  word-spacing: .35em;
  text-align: center;
  color: var(--fg);
  cursor: pointer;
  padding: 10px var(--gutter);
  border-radius: 10px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
}

button {
  font: inherit;
  color: var(--fg);
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
}
button:active { background: #232833; }
button.ghost { background: transparent; color: var(--fg-dim); }
button.ghost:active { color: var(--fg); }
button.small { padding: 4px 10px; font-size: 13px; }
button.danger { color: var(--danger); }

#btn-new {
  position: absolute;
  top: 12px;
  right: 0;
  bottom: 0;
  border-color: transparent;
  font-size: 20px;
  line-height: 1;
  padding: 0 16px;
}

/* ---------- timer ---------- */

#stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  min-height: 0;
  overflow: hidden;
}

#timer-area {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  min-height: 0;
}

#time {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: max(56px, min(24vw, 26vh, 190px));
  font-weight: 300;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  letter-spacing: -.02em;
  color: var(--fg);
  transition: color .08s linear;
}

/* "1:23.45" is two glyphs wider than "23.45" — step down so it still fits */
#time.long { font-size: max(44px, min(17vw, 20vh, 150px)); }

#hint {
  font-size: 13px;
  color: var(--fg-dim);
  height: 1.2em;
}

body[data-state="hold"]  #time { color: var(--hold); }
body[data-state="ready"] #time { color: var(--ready); }
body[data-state="run"]   #hint { visibility: hidden; }

body[data-state="hold"]  #cube-preview,
body[data-state="ready"] #cube-preview,
body[data-state="run"]   #cube-preview { opacity: 0; pointer-events: none; }

body[data-state="run"] #topbar,
body[data-state="run"] #stats,
body[data-state="run"] #list-wrap { opacity: .12; pointer-events: none; }

#topbar, #stats, #list-wrap, #cube-preview { transition: opacity .18s ease; }

/* ---------- cube net ---------- */

#cube-preview {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(9, 1fr);
  width: 156px;
  height: 117px;
  gap: 1px;
}

#cube-preview i {
  display: block;
  border-radius: 1.5px;
  background: #000;
}

#cube-preview i.U { background: var(--u); }
#cube-preview i.D { background: var(--d); }
#cube-preview i.F { background: var(--f); }
#cube-preview i.B { background: var(--b); }
#cube-preview i.L { background: var(--l); }
#cube-preview i.R { background: var(--r); }

/* ---------- stats ---------- */

#stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.stat {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 9px 10px;
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}
.stat .k { font-size: 11px; color: var(--fg-dim); text-transform: uppercase; letter-spacing: .08em; }
.stat .v { font-size: clamp(17px, 4.6vw, 22px); font-variant-numeric: tabular-nums; }
.stat .b { font-size: 11px; color: var(--fg-dim); font-variant-numeric: tabular-nums; }

/* ---------- list ---------- */

#list-wrap {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  max-height: 30vh;
  min-height: 84px;
}

#list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--fg-dim);
  border-bottom: 1px solid var(--line);
}

#list {
  list-style: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 5px;
  align-content: start;
}

#list li {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  gap: 6px;
  cursor: pointer;
}
#list li:active { border-color: var(--accent); }
#list li .n { color: var(--fg-dim); font-size: 11px; }
#list li.dnf .t { color: var(--fg-dim); text-decoration: line-through; }
#list li.best .t { color: var(--ready); }

/* ---------- detail sheet ---------- */

#sheet {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 20;
}
#sheet.hidden { display: none; }

#sheet-card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px;
  width: min(420px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#sheet-time { font-size: 38px; font-variant-numeric: tabular-nums; text-align: center; }
#sheet-scramble {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 13px;
  color: var(--fg-dim);
  text-align: center;
  line-height: 1.5;
  word-spacing: .3em;
}
#sheet-actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
#sheet-actions button { padding: 11px 0; }
#sheet-close { background: transparent; color: var(--fg-dim); border-color: transparent; }

/* ---------- mobile ---------- */

@media (max-width: 640px) {
  #stats { grid-template-columns: repeat(2, 1fr); }
  #cube-preview { width: 120px; height: 90px; }
  #list-wrap { max-height: 26vh; }
  #hint { font-size: 12px; }
}

@media (max-height: 520px) and (orientation: landscape) {
  #list-wrap, #cube-preview { display: none; }
  #time { font-size: clamp(48px, 14vh, 90px); }
  #stats { grid-template-columns: repeat(4, 1fr); }
}
