:root {
  color-scheme: light;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
  --ink: #182231;
  --muted: #64748b;
  --panel: #ffffff;
  --page: #f3f6f8;
  --line: #d9e1ea;
  --blue: #2167ae;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--page);
}

body {
  color: var(--ink);
  overscroll-behavior: none;
  touch-action: manipulation;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
}

.app-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: min(100%, 620px);
  height: 100dvh;
  margin: 0 auto;
  background: var(--page);
  border-inline: 1px solid var(--line);
}

.topbar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
  min-height: calc(56px + var(--safe-top));
  padding: var(--safe-top) 10px 0;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
}

.icon-button,
.close-button {
  border: 0;
  background: transparent;
  cursor: pointer;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  color: var(--muted);
  font-size: 1.55rem;
}

.icon-button:last-child {
  justify-self: end;
}

.icon-button:active {
  background: #edf2f6;
  transform: scale(0.94);
}

.icon-button:disabled {
  opacity: 0.2;
  pointer-events: none;
}

.gear {
  display: block;
  font-size: 1.35rem;
  line-height: 1;
}

.counter-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 9px;
  min-height: 0;
  overflow: hidden;
  background: var(--page);
}

.counter {
  --accent: #64748b;
  position: relative;
  display: grid;
  grid-template-columns: minmax(82px, 1fr) minmax(118px, 1.25fr) minmax(82px, 1fr);
  align-items: stretch;
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: linear-gradient(90deg, color-mix(in srgb, var(--accent) 7%, white), #ffffff 58%);
}

.counter::after {
  position: absolute;
  inset: 0;
  border-left: 4px solid var(--accent);
  content: "";
  pointer-events: none;
}

.counter-red { --accent: #c65a38; }
.counter-green { --accent: #4f7a50; }
.counter-colorless { --accent: #77716b; }
.counter-white { --accent: #a28a3e; }
.counter-blue { --accent: #3577a8; }
.counter-black { --accent: #6c6570; }
.counter-life { --accent: #b83f62; }
.counter-poison { --accent: #5f7b2f; }
.counter-storm { --accent: #70499b; }

.step-button {
  position: relative;
  z-index: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  color: color-mix(in srgb, var(--accent) 76%, var(--ink));
  cursor: pointer;
  font-size: clamp(2.2rem, 10vw, 3.5rem);
  font-weight: 260;
  line-height: 1;
  user-select: none;
  -webkit-user-select: none;
}

.step-button:active,
.step-button.is-held {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.counter-readout {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  pointer-events: none;
}

.counter-copy {
  min-width: 0;
  text-align: center;
}

.counter-name {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: clamp(0.66rem, 2.7vw, 0.78rem);
  font-weight: 780;
  letter-spacing: 0.11em;
  text-transform: uppercase;
  white-space: nowrap;
}

.counter-glyph {
  display: inline-grid;
  place-items: center;
  width: 25px;
  height: 25px;
  border: 1px solid color-mix(in srgb, var(--accent) 55%, transparent);
  border-radius: 50%;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0;
}

.mana-pip {
  display: block;
  width: 31px;
  height: 31px;
  margin: 0 auto 3px;
  filter: drop-shadow(0 1px 1px rgba(24, 34, 49, 0.12));
}

.counter-value {
  display: block;
  font-family: ui-rounded, "SF Pro Rounded", system-ui, sans-serif;
  font-size: clamp(3.45rem, 18vw, 6.5rem);
  font-variant-numeric: tabular-nums;
  font-weight: 760;
  letter-spacing: -0.075em;
  line-height: 0.92;
  text-indent: -0.075em;
  color: var(--ink);
}

.counter-list[data-density="compact"] {
  gap: 6px;
  padding: 7px;
}

.counter-list[data-density="compact"] .mana-pip {
  width: 25px;
  height: 25px;
  margin-bottom: 2px;
}

.counter-list[data-density="compact"] .counter-glyph {
  width: 22px;
  height: 22px;
}

.counter-list[data-density="compact"] .counter-name {
  gap: 6px;
  margin-bottom: 2px;
  font-size: 0.68rem;
}

.counter-list[data-density="compact"] .counter-value {
  font-size: clamp(2.5rem, 10vw, 4rem);
}

.counter-list[data-density="tight"] {
  gap: 4px;
  padding: 6px;
}

.counter-list[data-density="tight"] .counter {
  border-radius: 5px;
}

.counter-list[data-density="tight"] .step-button {
  font-size: clamp(1.8rem, 8vw, 2.5rem);
}

.counter-list[data-density="tight"] .mana-pip {
  width: 18px;
  height: 18px;
  margin-bottom: 1px;
}

.counter-list[data-density="tight"] .counter-glyph {
  width: 18px;
  height: 18px;
  font-size: 0.64rem;
}

.counter-list[data-density="tight"] .counter-name {
  gap: 4px;
  margin-bottom: 1px;
  font-size: 0.58rem;
  letter-spacing: 0.08em;
}

.counter-list[data-density="tight"] .counter-value {
  font-size: clamp(1.85rem, 8vw, 2.8rem);
}

.action-bar {
  padding: 8px 9px calc(8px + var(--safe-bottom));
  border-top: 1px solid var(--line);
  background: var(--page);
}

.new-turn-button {
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 46px;
  padding: 0 16px;
  border: 1px solid #cbd5df;
  border-radius: 7px;
  background: var(--panel);
  cursor: pointer;
  font-weight: 720;
  line-height: 1;
}

.new-turn-button:active {
  transform: scale(0.985);
  background: #edf3f8;
}

.settings-sheet {
  width: min(100% - 20px, 560px);
  max-height: min(760px, calc(100dvh - 24px));
  margin: auto auto 0;
  padding: 10px 18px calc(20px + var(--safe-bottom));
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 24px 24px 0 0;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 -20px 70px rgba(24, 34, 49, 0.22);
}

.settings-sheet::backdrop {
  background: rgba(24, 34, 49, 0.38);
  backdrop-filter: blur(3px);
}

.settings-sheet[open] {
  animation: sheet-in 180ms ease-out;
}

@keyframes sheet-in {
  from { transform: translateY(18px); opacity: 0; }
}

.sheet-handle {
  width: 42px;
  height: 4px;
  margin: 0 auto 16px;
  border-radius: 99px;
  background: #cbd5df;
}

.sheet-heading {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.close-button {
  min-height: 44px;
  padding-inline: 8px;
  color: var(--blue);
  font-weight: 760;
}

.counter-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 9px;
  margin: 4px 0 20px;
}

.counter-option {
  position: relative;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  min-height: 70px;
  padding: 8px 6px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #f7f9fb;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 700;
  text-align: center;
}

.counter-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.counter-option:has(input:checked) {
  border-color: color-mix(in srgb, var(--option-accent) 62%, #cbd5df);
  background: color-mix(in srgb, var(--option-accent) 9%, white);
  color: var(--ink);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--option-accent) 12%, transparent);
}

.option-pip,
.option-glyph {
  display: grid;
  place-items: center;
  width: 25px;
  height: 25px;
}

.option-glyph {
  border: 1px solid color-mix(in srgb, var(--option-accent) 50%, var(--line));
  border-radius: 50%;
  color: var(--option-accent);
  font-size: 0.76rem;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 16px 2px;
  border-top: 1px solid var(--line);
  cursor: pointer;
}

.setting-row strong,
.setting-row small {
  display: block;
}

.setting-row strong {
  font-size: 0.92rem;
}

.setting-row small {
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.76rem;
}

.switch {
  position: relative;
  flex: 0 0 auto;
  width: 48px;
  height: 28px;
}

.switch input {
  position: absolute;
  opacity: 0;
}

.switch-track {
  position: absolute;
  inset: 0;
  border-radius: 99px;
  background: #cbd5df;
  transition: 150ms ease;
}

.switch-track::after {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: white;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.3);
  content: "";
  transition: 150ms ease;
}

.switch input:checked + .switch-track {
  background: var(--blue);
}

.switch input:checked + .switch-track::after {
  transform: translateX(20px);
}

.reset-button {
  width: 100%;
  min-height: 48px;
  margin-top: 4px;
  border: 1px solid #e7b8b3;
  border-radius: 7px;
  background: #fff8f7;
  color: #b42318;
  cursor: pointer;
  font-weight: 700;
}

.announcement {
  position: fixed;
  top: -100px;
  left: -100px;
  width: 1px;
  height: 1px;
  overflow: hidden;
}

button:focus-visible,
input:focus-visible + .switch-track,
.counter-option:has(input:focus-visible) {
  outline: 3px solid var(--blue);
  outline-offset: 2px;
}

@media (min-width: 621px) {
  body {
    background: linear-gradient(180deg, #e8eef3, var(--page) 55%);
  }
}

@media (max-height: 650px) {
  .topbar {
    min-height: calc(50px + var(--safe-top));
  }

  .counter {
    min-height: 0;
  }

  .action-bar {
    padding-top: 7px;
    padding-bottom: calc(7px + var(--safe-bottom));
  }

  .new-turn-button {
    min-height: 42px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
