:root {
  --bg: #eef9ff;
  --ink: #142033;
  --muted: #4a5d72;
  --card: #fffdf6;
  --line: #1c2a3a;
  --accent: #ff2d6f;
  --accent-ink: #142033;
  --danger: #d62839;
  --topbar: #4da3ff;
  --game-cyan: #00e5d4;
  --game-lime: #c6ff1a;
  --game-yellow: #ffe014;
  --game-orange: #ff8a00;
  --game-violet: #9b6bff;
  --game-duty: #16d46b;
  --game-place: #2f8cff;
  --button-radius: 0px;
  --button-gap: 0px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --img-page: url("../img/bg-btn-candy-stripes.png");
  --bg-image-opacity: 0.4;
  font-size: 20px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background-color: var(--bg);
  background-image: none;
  color: var(--ink);
  font-family: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", "Heiti TC", sans-serif;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  background-color: transparent;
  background-image: var(--img-page);
  background-size: 240px;
  background-repeat: repeat;
  opacity: var(--bg-image-opacity);
  pointer-events: none;
}

body {
  height: 100dvh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: transparent;
}

a { color: inherit; text-decoration: none; }
.hidden { display: none !important; }

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  column-gap: 0.35rem;
  padding: 0.45rem 0.55rem;
  background: var(--topbar);
  color: var(--ink);
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 3px solid #142033;
  box-shadow: 0 4px 0 rgba(20, 32, 51, 0.18);
  overflow: hidden;
}

.topbar-name {
  font-size: 1.15rem;
  font-weight: 700;
  white-space: nowrap;
}
.topbar-title {
  justify-self: center;
  text-align: center;
  font-weight: 800;
  line-height: 1.15;
  white-space: nowrap;
  overflow: hidden;
  max-width: 100%;
  font-size: clamp(0.72rem, 3.6vw, 1rem);
  letter-spacing: 0.01em;
}
.topbar-version {
  font-weight: 650;
  font-size: 0.55em;
  opacity: 0.85;
  margin-left: 0.2em;
}
.topbar-link {
  justify-self: end;
  font-size: 0.58rem;
  font-weight: 600;
  line-height: 1.1;
  opacity: 0.75;
  border: 1px solid rgba(20, 32, 51, 0.45);
  border-radius: var(--button-radius);
  padding: 0.12rem 0.22rem;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.4);
  max-width: 100%;
  text-align: center;
  white-space: nowrap;
}

.flash-wrap { padding: 0.4rem 0.6rem 0; }
.flash {
  padding: 0.55rem 0.7rem;
  border: 2px solid var(--line);
  font-weight: 700;
  margin-bottom: 0.35rem;
  background: #fff;
}
.flash-error { background: #ffe3e8; }
.flash-ok { background: #e4ffe8; }

.page {
  width: min(100%, 520px);
  margin: 0 auto;
  padding: 0.55rem;
  padding-bottom: calc(0.7rem + var(--safe-bottom));
  flex: 1;
  min-height: 0;
  overflow: auto;
}

.page-full, .page-meters, .page-assign {
  width: min(100%, 560px);
  display: flex;
  flex-direction: column;
}
.page-meters, .page-assign { overflow: hidden; }
.page-full { overflow: auto; }

.panel { display: flex; flex-direction: column; gap: 0.5rem; min-height: 0; flex: 1; }
.meters-panel { gap: 0.28rem; }
.panel-full { min-height: 0; height: 100%; }
.panel-narrow { margin-top: 1.5rem; }

.screen-title {
  margin: 0;
  font-size: 1.25rem;
  text-align: center;
  font-weight: 800;
}
.screen-title-brand {
  font-size: 1.55rem;
  font-weight: 900;
  margin-bottom: 0.2rem;
}
.screen-sub {
  margin: 0 0 0.5rem;
  text-align: center;
  font-weight: 650;
  color: var(--muted);
}
.screen-bar {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 0.5rem;
}
.screen-bar-sticky {
  position: static;
  background: transparent;
  padding: 0;
}
.back-link { font-size: 1.05rem; color: var(--accent); font-weight: 800; white-space: nowrap; }

.grid {
  display: grid;
  gap: var(--button-gap);
}
.grid-3x4 { grid-template-columns: repeat(3, 1fr); }

.tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 4.6rem;
  border: 3px solid var(--line);
  background: #e7eef2;
  font-weight: 800;
  text-align: center;
  padding: 0.35rem;
  -webkit-tap-highlight-color: transparent;
}
.tile-user { background: var(--game-cyan); }
.tile-blank {
  background: #d8dee2;
  visibility: visible;
  pointer-events: none;
}
.tile-text { font-size: 1.35rem; font-weight: 900; }
.tile-pick {
  cursor: pointer;
  background: #fff;
  flex-direction: column;
  gap: 0.15rem;
}
.tile-pick .pick-check { position: absolute; opacity: 0; pointer-events: none; }
.tile-pick.is-selected {
  background: var(--game-lime);
  box-shadow: inset 0 0 0 3px var(--accent);
}
.tile-meter {
  font-size: 0.95rem;
  font-weight: 900;
  color: var(--muted);
}
.tile-meter.is-pos { color: #0b7a3a; }
.tile-meter.is-neg { color: var(--danger); }

.btn {
  appearance: none;
  border: 3px solid var(--line);
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  min-height: 2.8rem;
  padding: 0.4rem 0.7rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-ghost { background: #fff; }
.btn-block { width: 100%; min-height: 3.2rem; }
.btn-sm { min-height: 2.2rem; font-size: 0.95rem; }
.btn-action {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 3.4rem;
  font-size: 1.15rem;
  font-weight: 900;
  border: 3px solid var(--line);
}
.btn-action-assign { background: var(--game-duty); }
.btn-action-helpers { background: var(--game-violet); color: #fff; }
.btn-action-export { background: var(--game-lime); }
.btn-action-stats { background: var(--game-place); color: #fff; }

.field-label { display: block; font-weight: 800; margin: 0.4rem 0 0.25rem; }
.field-input {
  width: 100%;
  font: inherit;
  font-weight: 700;
  padding: 0.65rem 0.7rem;
  border: 3px solid var(--line);
  margin-bottom: 0.55rem;
}

.period-bar { display: grid; gap: 0.28rem; flex: none; }
.period-tabs {
  display: flex;
  align-items: stretch;
  gap: 0.18rem;
}
.period-tab {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.02rem;
  min-height: 2.15rem;
  padding: 0.1rem 0.2rem;
  border: 2px solid var(--line);
  background: #fff;
  font-weight: 800;
  font-size: 0.82rem;
  cursor: pointer;
  line-height: 1.05;
  white-space: nowrap;
}
.period-tab input { display: none; }
.period-tab.is-on { background: var(--game-yellow); }
.period-tab-label { white-space: nowrap; }
.period-tab-range {
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0;
  white-space: nowrap;
}
.custom-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  align-items: center;
}
.custom-fields input[type="date"] {
  font: inherit;
  font-size: 0.75rem;
  border: 2px solid var(--line);
  padding: 0.2rem;
  font-weight: 700;
}
.period-range {
  margin: 0;
  text-align: center;
  font-weight: 700;
  color: var(--muted);
  font-size: 0.95rem;
}

.meters-split {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 5.2rem;
  gap: 0.28rem;
}
.meter-list {
  display: grid;
  grid-template-rows: repeat(var(--meter-rows, 10), minmax(0, 1fr));
  gap: 0.18rem;
  min-height: 0;
  overflow: hidden;
}
.meter-row {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.35rem;
  padding: 0 0.4rem;
  min-height: 0;
  overflow: hidden;
  border: 2px solid var(--line);
  background: rgba(255, 255, 255, 0.92);
  appearance: none;
  font: inherit;
  color: inherit;
  text-align: left;
  width: 100%;
  cursor: pointer;
}
.meter-bar {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  background: #b9e3ff;
  z-index: 0;
  pointer-events: none;
}
.meter-name {
  position: relative;
  z-index: 1;
  font-size: clamp(0.62rem, 1.9vh, 0.82rem);
  font-weight: 800;
  line-height: 1;
}
.meter-total {
  position: relative;
  z-index: 1;
  font-size: clamp(0.62rem, 1.9vh, 0.82rem);
  font-weight: 800;
  min-width: 1.4rem;
  text-align: right;
  line-height: 1;
}
.meter-total.is-pos { color: #0b7a3a; }
.meter-total.is-neg { color: var(--danger); }

.admin-side {
  display: flex;
  flex-direction: column;
  gap: 0.28rem;
  min-height: 0;
}
.admin-side .btn-action {
  flex: 1;
  min-height: 0;
  font-size: 0.82rem;
  padding: 0.15rem 0.1rem;
  line-height: 1.1;
}

.assign-panel { flex: 1; min-height: 0; height: auto; overflow: hidden; }
.assign-head {
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
  flex: none;
  min-width: 0;
}
.assign-head .back-link { font-size: 0.78rem; }
.assign-head .screen-title { font-size: 0.92rem; flex: none; text-align: left; }
.period-inline {
  margin: 0;
  font-size: 0.64rem;
  font-weight: 700;
  color: var(--muted);
  white-space: nowrap;
  flex: none;
}
.assign-form { display: flex; flex-direction: column; gap: 0.28rem; flex: 1; min-height: 0; }
.assign-pick-grid {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  align-content: stretch;
  grid-template-rows: repeat(var(--pick-rows, 4), minmax(0, 1fr));
}
.assign-pick-grid .tile { min-height: 0; height: 100%; padding: 0.1rem; }
.assign-pick-grid .tile-text { font-size: clamp(0.85rem, 3.2vh, 1.25rem); }
.assign-pick-grid .tile-meter { font-size: clamp(0.62rem, 2vh, 0.85rem); }
.assign-next { margin: 0; flex: none; min-height: 2rem; }

.bulk-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.22rem;
  flex: none;
}
.bulk-btn {
  min-height: 1.7rem;
  background: var(--game-orange);
  font-size: 1rem;
  font-weight: 900;
  padding: 0.05rem;
}

.points-form { display: flex; flex-direction: column; flex: 1; min-height: 0; gap: 0.25rem; }
.points-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(var(--point-rows, 1), minmax(0, 1fr));
  gap: 0.16rem;
  margin: 0;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
.points-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.2rem;
  align-items: center;
  border: 2px solid var(--line);
  background: #fff;
  padding: 0.12rem 0.28rem;
  min-height: 0;
  overflow: hidden;
}
.points-who {
  display: flex;
  gap: 0.28rem;
  align-items: baseline;
  white-space: nowrap;
  min-width: 0;
  font-size: clamp(0.72rem, 2.1vh, 0.95rem);
  font-weight: 800;
}
.points-who strong,
.points-total {
  font-size: 1em;
  font-weight: 800;
  line-height: 1;
}
.points-ctrl {
  display: grid;
  grid-template-columns: 1.15rem 1.45rem 1.15rem;
  gap: 0.08rem;
  align-items: center;
  justify-self: end;
  height: 1.15rem;
  max-height: 100%;
}
.points-ctrl .pts-value,
.points-ctrl .pts-btn {
  width: 100%;
  height: 1.15rem;
  min-height: 0;
  max-height: 100%;
  font-size: 0.78rem;
  font-weight: 900;
  line-height: 1;
  padding: 0;
  border-width: 2px;
}
.pts-value {
  text-align: center;
  font: inherit;
  background: var(--game-yellow);
}
.remark-row {
  display: flex;
  gap: 0.28rem;
  align-items: center;
  flex: none;
}
.remark-row .field-input {
  flex: 1;
  min-width: 0;
  margin: 0;
  min-height: 2rem;
  padding: 0.3rem 0.45rem;
}
.remark-confirm { flex: none; min-height: 2rem; white-space: nowrap; padding: 0.2rem 0.45rem; }

.recent-strip {
  margin-top: auto;
  height: 20%;
  min-height: 6.5rem;
  max-height: 8.5rem;
  display: flex;
  flex-direction: column;
  border-top: 3px solid var(--line);
  padding-top: 0.3rem;
  background: rgba(255, 255, 255, 0.72);
}
.recent-title {
  margin: 0 0 0.2rem;
  font-size: 0.95rem;
  font-weight: 900;
}
.recent-scroll {
  overflow: auto;
  flex: 1;
  font-size: 0.88rem;
  font-weight: 700;
}
.recent-item {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0.15rem 0;
  border-bottom: 1px dashed #c5d0d6;
}
.recent-note { color: var(--muted); }
.is-pos { color: #0b7a3a; }
.is-neg { color: var(--danger); }
.empty-hint { color: var(--muted); font-weight: 700; text-align: center; }

.page-helpers { overflow: hidden; }
.helper-form { display: flex; flex-direction: column; flex: 1; min-height: 0; gap: 0.3rem; }
.helper-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(var(--helper-rows, 5), minmax(0, 1fr));
  gap: 0.22rem;
  margin: 0;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
.helper-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.35rem;
  align-items: center;
  padding: 0.15rem 0.45rem;
  min-height: 0;
  border: 2px solid var(--line);
  background: #fff;
  font-weight: 800;
}
.helper-row input { width: 1rem; height: 1rem; }
.helper-name { font-size: 1.05rem; font-weight: 900; }
.helper-save { flex: none; min-height: 2.2rem; }
.history-title-note { font-size: 0.72em; font-weight: 700; color: var(--muted); }
.history-list { display: grid; gap: 0.25rem; margin: 0 0 0.8rem; max-height: 14rem; overflow: auto; }
.history-line { margin: 0; font-size: 0.9rem; font-weight: 700; }
.recent-line {
  flex: none;
  border-top: 2px solid var(--line);
  background: rgba(255, 255, 255, 0.86);
}
.recent-line-scroll {
  overflow-x: auto;
  white-space: nowrap;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.55rem;
  padding: 0 0.15rem;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(18, 28, 34, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 50;
}
.modal-card {
  width: min(100%, 22rem);
  background: #fff;
  border: 3px solid var(--line);
  padding: 1rem;
}
.modal-message {
  margin: 0 0 1rem;
  font-size: 1.25rem;
  font-weight: 800;
  text-align: center;
}
.modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
}

.password-form { margin-top: 0.5rem; }

@media (min-width: 720px) {
  .page { width: min(100%, 560px); }
}
