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

:root {
  --bg: #0b1220;
  --surface: #151f32;
  --surface-2: #1e2d45;
  --border: #2a3a52;
  --text: #e8eef7;
  --muted: #7a8ba3;
  --primary: #22c55e;
  --primary-dark: #16a34a;
  --danger: #ef4444;
  --warning: #f59e0b;
  --accent: #3b82f6;
  --radius: 8px;
  --gap: 6px;
  --topbar-h: 40px;
  --roundbar-h: 38px;
  --statusbar-h: 22px;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  font-size: 13px;
}

html, body {
  height: 100%;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

body {
  background: var(--bg);
  color: var(--text);
  line-height: 1.3;
}

/* ── App shell: fixed 100dvh, no page scroll ── */
.app {
  display: grid;
  grid-template-rows: var(--topbar-h) var(--roundbar-h) 1fr var(--statusbar-h);
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ── Top bar ── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: var(--topbar-h);
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar-brand h1 {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.badge {
  background: var(--primary);
  color: #052e16;
  font-size: 9px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.topbar-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.storage-dot {
  color: var(--primary);
  font-weight: 600;
  font-size: 10px;
}

/* ── Round bar ── */
.round-bar {
  display: flex;
  align-items: center;
  padding: 4px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: var(--roundbar-h);
}

.round-tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  width: 100%;
}

.round-tab {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 5px 2px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  font-weight: 700;
  transition: border-color 0.12s, background 0.12s;
}

.round-tab.active {
  border-color: var(--primary);
  background: rgba(34, 197, 94, 0.15);
  color: var(--primary);
}

.round-tab-count {
  font-size: 9px;
  background: var(--surface-2);
  border-radius: 999px;
  padding: 0 5px;
  min-width: 16px;
  text-align: center;
  color: var(--muted);
}

.round-tab.active .round-tab-count {
  background: var(--primary);
  color: #052e16;
}

/* ── Dashboard grid ── */
.dashboard {
  display: grid;
  grid-template-columns: minmax(0, 38%) minmax(0, 1fr);
  gap: var(--gap);
  padding: var(--gap);
  min-height: 0;
  overflow: hidden;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-height: 0;
  overflow: hidden;
}

/* ── Control panel (left) ── */
.control-panel {
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: var(--gap);
  padding: 8px;
}

.clock-hint {
  font-size: 9px;
  color: var(--muted);
  margin-top: 2px;
}

.mini-stats {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
  align-self: end;
}

.panel-label {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: 2px;
}

.timer-block {
  text-align: center;
}

.clock-display {
  font-size: clamp(1.6rem, 7vw, 2.4rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--primary);
  line-height: 1;
  letter-spacing: 0.03em;
}

.race-status {
  font-size: 10px;
  color: var(--muted);
  font-weight: 600;
}

.status-idle { color: var(--muted); }
.status-running { color: var(--primary); font-weight: 600; }

.bib-block .entry-row {
  display: flex;
  gap: 4px;
}

.entry-row input {
  flex: 1;
  min-width: 0;
  font-size: 1.25rem;
  font-weight: 700;
  text-align: center;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.entry-row input:focus,
.entry-row input.kb-open {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.25);
}

.entry-row input.bib-duplicate {
  border-color: var(--warning);
  box-shadow: 0 0 0 2px rgba(245, 158, 11, 0.35);
  color: var(--warning);
}

.bib-warning {
  margin-top: 4px;
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 10px;
  font-weight: 700;
  color: #fde68a;
  background: rgba(245, 158, 11, 0.15);
  border: 1px solid rgba(245, 158, 11, 0.45);
}

.bib-warning[hidden] {
  display: none;
}

.format-hint {
  font-weight: 400;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0;
}

.entry-row input:disabled {
  opacity: 0.4;
}

.mini-stat {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 6px;
  text-align: center;
}

.mini-stat-val {
  display: block;
  font-size: 16px;
  font-weight: 800;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.mini-stat-lbl {
  font-size: 8px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* ── Log panel (right) ── */
.log-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.log-toolbar {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.log-toolbar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
}

.log-toolbar-bottom {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.log-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
  white-space: nowrap;
  flex-shrink: 0;
}

.toolbar-select {
  flex: 1;
  min-width: 0;
  max-width: 220px;
  padding: 7px 28px 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 11px;
  font-weight: 600;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%237a8ba3' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  cursor: pointer;
}

.toolbar-search {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 11px;
}

.toolbar-search::placeholder {
  color: var(--muted);
}

.toolbar-search:focus,
.toolbar-search.kb-open {
  border-color: var(--primary);
  outline: none;
  box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.2);
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  padding: 3px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.btn-tool {
  width: 38px;
  height: 38px;
  padding: 0;
  font-size: 16px;
  line-height: 1;
  border-radius: 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  touch-action: manipulation;
}

.toolbar-actions .btn-danger {
  border: none;
  background: rgba(239, 68, 68, 0.12);
}

.log-table-wrap {
  flex: 1;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.log-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}

.log-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  text-align: left;
  color: var(--muted);
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 4px 5px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.log-table td {
  padding: 4px 5px;
  border-bottom: 1px solid rgba(42, 58, 82, 0.6);
  font-variant-numeric: tabular-nums;
}

.log-table .bib-cell {
  font-weight: 700;
  color: var(--warning);
}

.log-table .time-cell {
  font-weight: 700;
  color: var(--primary);
}

.empty-row td {
  text-align: center;
  color: var(--muted);
  padding: 12px 4px;
  font-size: 10px;
}

.btn-delete {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 14px;
  padding: 0 3px;
  line-height: 1;
  opacity: 0.75;
}

/* ── Buttons ── */
.btn {
  border: none;
  border-radius: 6px;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.12s, transform 0.08s;
  white-space: nowrap;
}

.btn:active:not(:disabled) {
  transform: scale(0.96);
}

.btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.btn-compact {
  font-size: 12px;
  padding: 7px 8px;
}

.btn-primary {
  background: var(--primary);
  color: #052e16;
}

.btn-record {
  background: var(--warning);
  color: #451a03;
  min-width: 36px;
  font-size: 16px;
  padding: 6px;
}

.btn-ghost {
  background: var(--surface-2);
  color: var(--text);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--danger);
}

.btn-share {
  background: var(--accent);
  color: #eff6ff;
}

.btn-xs {
  font-size: 12px;
  padding: 4px 7px;
  border-radius: 5px;
  flex-shrink: 0;
}

/* ── Status bar ── */
.statusbar {
  display: flex;
  align-items: center;
  padding: 0 10px;
  font-size: 9px;
  color: var(--muted);
  background: var(--surface);
  border-top: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: calc(var(--statusbar-h) + 8px);
  left: 50%;
  transform: translateX(-50%) translateY(120%);
  background: var(--surface-2);
  color: var(--text);
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  transition: transform 0.25s ease;
  z-index: 100;
  pointer-events: none;
  white-space: nowrap;
  max-width: 90vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
}

.toast.toast-warning {
  background: rgba(245, 158, 11, 0.96);
  color: #451a03;
  border: 1px solid #fbbf24;
  box-shadow: 0 4px 24px rgba(245, 158, 11, 0.35);
  white-space: normal;
  text-align: center;
  border-radius: 12px;
  max-width: 92vw;
}

/* ── Custom bib keyboard ── */
.bib-keyboard {
  position: relative;
  z-index: 1;
  transform: translateY(0);
  overflow: visible;
}

/* Mobile: floating keyboard at bottom */
body.mode-touch .bib-keyboard {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 300;
  background: var(--surface);
  border-top: 2px solid var(--primary);
  padding: 14px 8px calc(8px + env(safe-area-inset-bottom));
  transform: translateY(100%);
  transition: transform 0.15s ease;
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.45);
  display: block;
}

body.mode-touch .bib-keyboard:not(.open) {
  pointer-events: none;
}

body.mode-touch .bib-keyboard.open {
  transform: translateY(0);
}

/* Desktop: embedded in sidebar */
body.mode-desktop .bib-keyboard {
  display: block !important;
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
  background: transparent;
  box-shadow: none;
}

body.mode-desktop .bib-keyboard.open {
  transform: none;
}

body.mode-desktop .kb-handle {
  display: none;
}

body.mode-desktop .kb-key {
  padding: 7px 2px;
  font-size: 0.9rem;
}

body.mode-desktop .kb-preview {
  font-size: 1.05rem;
  padding: 4px 6px;
}

body.mode-desktop .kb-backspace {
  width: 36px;
}

body.mode-desktop .kb-header {
  margin-bottom: 4px;
}

body.mode-desktop .kb-row,
body.mode-desktop .kb-grid {
  gap: 4px;
  margin-bottom: 4px;
}

body.mode-desktop .control-panel {
  grid-template-rows: auto 1fr auto;
}

body.mode-desktop .entry-row input {
  cursor: text;
  text-transform: uppercase;
}

body.mode-desktop .bib-block {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.kb-handle {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1.5px solid #2f4058;
  background: radial-gradient(circle at 35% 30%, #243044 0%, #141f30 55%, #0e1624 100%);
  box-shadow:
    0 4px 12px rgba(0, 0, 0, 0.5),
    inset 0 1px 3px rgba(255, 255, 255, 0.07),
    inset 0 -3px 6px rgba(0, 0, 0, 0.4);
  cursor: pointer;
  z-index: 201;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.kb-handle:active {
  transform: translate(-50%, -46%);
  box-shadow:
    0 2px 8px rgba(0, 0, 0, 0.4),
    inset 0 2px 6px rgba(0, 0, 0, 0.45);
}

.kb-handle-chevron {
  width: 16px;
  height: 16px;
  color: #6ee7b7;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.45));
}

.kb-inner {
  padding-top: 4px;
}

.kb-header {
  display: flex;
  align-items: stretch;
  gap: 6px;
  margin-bottom: 6px;
}

.kb-backspace {
  flex-shrink: 0;
  width: 44px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: #cbd5e1;
  cursor: pointer;
  touch-action: manipulation;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.kb-backspace-icon {
  width: 22px;
  height: 22px;
  display: block;
}

.kb-backspace:active {
  transform: scale(0.96);
  background: rgba(148, 163, 184, 0.15);
  color: #f1f5f9;
}

.kb-preview {
  flex: 1;
  text-align: center;
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  padding: 6px 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}

.kb-preview.kb-preview-warn {
  color: var(--warning);
  border-color: var(--warning);
  background: rgba(245, 158, 11, 0.1);
}

.kb-row {
  display: grid;
  gap: 5px;
  margin-bottom: 5px;
}

.kb-letters {
  grid-template-columns: repeat(4, 1fr);
}

.kb-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
}

.kb-key {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 1.15rem;
  font-weight: 700;
  padding: 12px 4px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.kb-key:active {
  transform: scale(0.96);
  background: var(--border);
}

.kb-key-letter {
  background: rgba(59, 130, 246, 0.2);
  border-color: var(--accent);
  color: #93c5fd;
  font-size: 1.2rem;
}

.kb-key-letter.selected {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.kb-key-letter:disabled {
  opacity: 0.35;
}

.kb-key-clear {
  background: rgba(239, 68, 68, 0.15);
  border-color: var(--danger);
  color: #fca5a5;
  font-size: 0.75rem;
  letter-spacing: 0.02em;
}

.kb-key-done {
  background: var(--primary);
  border-color: var(--primary);
  color: #052e16;
  font-size: 1.4rem;
  line-height: 1;
}

.kb-key-done.kb-done-blocked {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--muted);
  opacity: 0.55;
}

@media (max-width: 600px) {
  .kb-key {
    padding: 14px 4px;
    font-size: 1.25rem;
  }

  .kb-preview {
    font-size: 1.45rem;
  }
}

.clock-hint-touch {
  display: none;
}

body.mode-touch .clock-hint-touch {
  display: block;
}

body.mode-touch .clock-hint-desktop {
  display: none;
}

body.mode-desktop .clock-hint-desktop {
  display: block;
}

body.mode-touch .entry-row input {
  cursor: pointer;
  caret-color: transparent;
  touch-action: manipulation;
  min-height: 44px;
  -webkit-tap-highlight-color: rgba(34, 197, 94, 0.15);
}

body.mode-touch .toolbar-search {
  touch-action: manipulation;
  min-height: 38px;
  -webkit-tap-highlight-color: rgba(34, 197, 94, 0.15);
}

.desktop-only,
.btn-enter {
  display: none !important;
}

body.mode-desktop .desktop-only,
body.mode-desktop .btn-enter {
  display: inline-flex !important;
}

body.mode-desktop .btn-enter {
  width: 100%;
  margin-top: 4px;
  justify-content: center;
  font-size: 0.8rem;
  padding: 6px;
}

@media (max-width: 768px) {
  .desktop-only,
  .btn-enter {
    display: none !important;
  }

  body.mode-touch .bib-keyboard {
    display: block;
  }
}

@media (min-width: 601px) and (max-width: 768px) {
  body.mode-touch .bib-keyboard {
    left: 50%;
    transform: translateX(-50%) translateY(100%);
    max-width: 360px;
    border-radius: 14px 14px 0 0;
  }

  body.mode-touch .bib-keyboard.open {
    transform: translateX(-50%) translateY(0);
  }
}

/* ── Mobile: stack controls on top, log below ── */
@media (max-width: 600px) {
  :root {
    --topbar-h: 36px;
    --roundbar-h: 34px;
    --statusbar-h: 20px;
  }

  .dashboard {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .control-panel {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto;
    gap: 4px;
    padding: 6px;
  }

  .timer-block {
    grid-column: 1;
    text-align: left;
  }

  .clock-display {
    font-size: 1.5rem;
  }

  .bib-block {
    grid-column: 2;
    grid-row: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .bib-block .entry-row input {
    font-size: 1.1rem;
    padding: 5px;
  }

  .mini-stats {
    grid-column: 1 / -1;
    grid-row: 2;
    gap: 3px;
  }

  .mini-stat-val {
    font-size: 13px;
  }
}

/* ── Very short screens ── */
@media (max-height: 520px) {
  .clock-display {
    font-size: 1.3rem;
  }

  .control-panel {
    padding: 4px 6px;
  }

  .mini-stat-val {
    font-size: 12px;
  }
}

/* ── Wide screens ── */
@media (min-width: 900px) {
  .app {
    max-width: 960px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }

  .dashboard {
    grid-template-columns: 280px 1fr;
  }
}
