:root {
  --bg: #f6f3ec;
  --ink: #151515;
  --muted: #6e6a62;
  --line: #d7d0c2;
  --white: #fffdf7;
  --black: #171717;
  --black-soft: #262626;
  --teal: #0f9f8f;
  --teal-strong: #067967;
  --coral: #e15649;
  --gold: #d4a72c;
  --shadow: 0 18px 45px rgba(21, 21, 21, 0.14);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  overscroll-behavior: none;
}

body {
  min-height: 100svh;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app {
  height: var(--app-height, 100dvh);
  min-height: var(--app-height, 100dvh);
  max-height: var(--app-height, 100dvh);
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  gap: 10px;
  overflow: hidden;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

.topbar,
.controlbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.topbar {
  flex-wrap: wrap;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 40px;
  padding-right: 4px;
  color: var(--ink);
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0;
}

.brand-mark {
  display: grid;
  width: 30px;
  height: 30px;
  place-items: center;
  border-radius: 50%;
  background: var(--ink);
  color: var(--white);
  line-height: 1;
}

.preset-strip {
  display: flex;
  flex: 1 1 260px;
  gap: 6px;
  min-width: 0;
  overflow-x: auto;
  padding-bottom: 1px;
  scrollbar-width: none;
}

.preset-strip::-webkit-scrollbar {
  display: none;
}

.preset,
.command,
.icon-command {
  min-height: 40px;
  border-radius: 8px;
  color: var(--ink);
  background: #e7e0d1;
  font-weight: 800;
  transition:
    transform 120ms ease,
    background 120ms ease,
    color 120ms ease,
    box-shadow 120ms ease;
}

.preset {
  flex: 0 0 auto;
  min-width: 58px;
  padding: 0 13px;
}

.preset.is-selected {
  background: var(--ink);
  color: var(--white);
}

.custom-controls {
  display: flex;
  gap: 6px;
}

.custom-controls label {
  display: grid;
  grid-template-columns: auto 54px;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 4px 7px 4px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.55);
  font-size: 13px;
  font-weight: 800;
}

.custom-controls input {
  width: 54px;
  height: 30px;
  border: 0;
  border-radius: 6px;
  padding: 0 6px;
  text-align: center;
  color: var(--ink);
  background: var(--white);
  font-weight: 900;
}

.clock-board {
  display: grid;
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  gap: 10px;
  min-height: 0;
  min-width: 0;
}

.player {
  position: relative;
  display: grid;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  border-radius: 8px;
  box-shadow: var(--shadow);
  touch-action: manipulation;
  user-select: none;
}

.player::before {
  content: "";
  position: absolute;
  inset: auto 0 0;
  height: calc(var(--left, 100) * 1%);
  opacity: 0.2;
  transition: height 140ms linear;
}

.player-inner {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-rows: auto 1fr auto;
  align-items: center;
  width: 100%;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  padding: clamp(14px, 3svh, 30px);
}

.player-meta {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  width: 100%;
  font-size: clamp(15px, 2.4svh, 22px);
  font-weight: 900;
}

.move-count,
.turn-label {
  opacity: 0.66;
}

.time {
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  justify-self: center;
  font-variant-numeric: tabular-nums;
  font-size: clamp(52px, min(22vw, 15svh), 116px);
  font-weight: 950;
  letter-spacing: 0;
  line-height: 0.9;
}

.bonus {
  position: absolute;
  left: clamp(14px, 4vw, 34px);
  bottom: clamp(14px, 3svh, 28px);
  z-index: 2;
  min-width: 74px;
  padding: 7px 10px;
  border-radius: 8px;
  background: var(--teal);
  color: var(--white);
  font-size: clamp(17px, 4.8vw, 28px);
  font-weight: 950;
  line-height: 1;
  opacity: 0;
  pointer-events: none;
  text-align: center;
  transform: translateY(8px) scale(0.8);
}

.bonus.is-visible {
  animation: bonusPop 850ms ease-out;
}

.player-black .bonus {
  background: var(--gold);
  color: var(--black);
}

@keyframes bonusPop {
  0% {
    opacity: 0;
    transform: translateY(10px) scale(0.8);
  }

  18% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }

  72% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }

  100% {
    opacity: 0;
    transform: translateY(-16px) scale(0.92);
  }
}

.turn-label {
  justify-self: end;
  min-height: 28px;
  font-size: clamp(16px, 2.6svh, 24px);
  font-weight: 900;
}

.player-white {
  color: var(--ink);
  background:
    linear-gradient(135deg, rgba(15, 159, 143, 0.12), transparent 42%),
    var(--white);
}

.player-white::before {
  background: var(--teal);
}

.player-black {
  color: var(--white);
  background:
    linear-gradient(135deg, rgba(212, 167, 44, 0.14), transparent 40%),
    var(--black);
}

.player-black::before {
  background: var(--gold);
}

.player.is-active {
  outline: 4px solid var(--teal);
  outline-offset: -4px;
}

.player.is-active .turn-label {
  opacity: 1;
  color: var(--teal);
}

.player-black.is-active .turn-label {
  color: var(--gold);
}

.player.is-low {
  outline-color: var(--coral);
}

.player.is-low .turn-label {
  color: var(--coral);
}

.player.is-flagged {
  outline-color: var(--coral);
  animation: flagPulse 650ms ease-in-out infinite alternate;
}

@keyframes flagPulse {
  from {
    filter: saturate(1);
  }

  to {
    filter: saturate(1.35) brightness(1.04);
  }
}

.controlbar {
  flex-wrap: wrap;
}

.command {
  flex: 1 1 116px;
  padding: 0 14px;
}

.command.primary {
  background: var(--teal);
  color: var(--white);
}

.icon-command {
  flex: 0 0 44px;
  width: 44px;
  padding: 0;
  font-size: 20px;
}

.icon-command.danger {
  background: #f3cbc6;
  color: #9b261d;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 40px;
  padding: 0 10px;
  border-radius: 8px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.58);
  font-size: 14px;
  font-weight: 900;
  white-space: nowrap;
}

.toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--teal);
}

.toggle.is-disabled {
  opacity: 0.52;
}

.status {
  min-height: 20px;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  text-align: center;
}

button:active {
  transform: scale(0.985);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
  transform: none;
}

button:focus-visible,
input:focus-visible {
  outline: 3px solid var(--coral);
  outline-offset: 2px;
}

.app.table-mode .player-black .player-inner {
  transform: rotate(180deg);
}

@media (orientation: landscape) {
  .app {
    grid-template-rows: auto 1fr auto;
    gap: 8px;
  }

  .clock-board {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
  }

  .player-black {
    order: 2;
  }

  .player-white {
    order: 1;
  }

  .app.table-mode .player-black .player-inner {
    transform: none;
  }

  .time {
    font-size: clamp(48px, min(10vw, 24svh), 120px);
  }

  .bonus {
    left: clamp(12px, 2vw, 22px);
    bottom: clamp(12px, 3svh, 20px);
    font-size: clamp(16px, 3vw, 24px);
  }

  .player-inner {
    padding: clamp(12px, 3svh, 22px);
  }

  .status {
    display: none;
  }
}

@media (max-width: 520px) {
  .app {
    gap: 8px;
  }

  .topbar {
    align-items: stretch;
  }

  .brand {
    flex: 0 0 auto;
  }

  .custom-controls {
    width: 100%;
  }

  .custom-controls label {
    flex: 1 1 0;
  }

  .controlbar {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }

  .command {
    grid-column: span 2;
  }

  .icon-command {
    width: 100%;
  }

  .toggle {
    grid-column: span 2;
  }

  .toggle {
    justify-content: center;
  }
}

@media (max-height: 560px) and (orientation: portrait) {
  .brand {
    display: none;
  }

  .custom-controls {
    display: none;
  }

  .player-inner {
    padding: 14px;
  }

  .time {
    font-size: clamp(48px, 15svh, 96px);
  }
}
