:root {
  --bg: #0b0f14;
  --card: #111823;
  --text: #e7eef7;
  --muted: #93a0b4;
  --accent: #2dd4bf;
  --accent-2: #6366f1;
  --cell: 2rem;
  --gap: 6px;
  --pad-tile: 72px; /* computed per layout for emoji pad */
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}
/* iOS text auto-size can cause layout zoom; keep at 100% */
html { -webkit-text-size-adjust: 100%; }
/* Prevent accidental horizontal page scroll due to wide inner scrollers */
html, body { max-width: 100%; overflow-x: hidden; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top, 0px)) 16px 12px 16px;
  background: var(--card);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 18px; margin: 0; letter-spacing: 0.5px; }
.match-name { color: var(--muted); font-size: 14px; margin-left: auto; }
.match-name strong { color: #e7eef7; font-weight: 700; }
.match-name .sep { color: #64748b; margin: 0 6px; }
.topbar .btn { margin-left: auto; margin-right: 0; }

.container { padding: 12px 16px 16px; }
main.container { max-width: 980px; margin: 0 auto; }
/* Ensure global view always fits the viewport width */
#global-view { width: 100%; max-width: 100%; box-sizing: border-box; }
#panel-replay { width: 100%; max-width: 100%; box-sizing: border-box; }
/* Reserve space for the emoji pad when visible (dynamic via --pad-h) */
body.pad-visible .container { padding-bottom: calc(var(--pad-h, 160px) + env(safe-area-inset-bottom, 0px)); }

.status { display: grid; gap: 12px; }
.ball-label {
  font-size: 64px;
  font-weight: 900;
  text-align: center;
  line-height: 1.1;
  letter-spacing: -0.5px;
  margin: 6px 0 10px;
}
.controls { display: flex; gap: 8px; justify-content: center; }
.ball-header { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 8px; }
.innings-chip { justify-self: center; font-size: 12px; color: #cbd5e1; background: #0f172a; border: 1px solid #1f2a44; padding: 4px 8px; border-radius: 999px; }
.btn {
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 16px;
  border: 1px solid #2a3a55;
  color: #eaf2ff;
  background: #1e293b; /* readable default for Back / Leave */
}
.btn.primary { background: var(--accent-2); color: #fff; border-color: #5458df; }
.btn.secondary { background: #22314a; color: #e2e8f0; border-color: #2f466b; }
.btn.ghost { background: transparent; color: #b6c2d6; border-color: #2a3a55; }
.btn.sm { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.btn.wide { width: 100%; margin-top: 6px; }
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px); filter: brightness(0.96); }
.btn:focus-visible { outline: 2px solid #93c5fd; outline-offset: 2px; }

.timeline h2 { font-size: 16px; color: var(--muted); margin: 8px 0; }
.timeline-grid {
  display: grid;
  grid-template-rows: repeat(6, var(--cell));
  grid-template-columns: repeat(20, var(--cell));
  gap: var(--gap);
  padding-bottom: 8px;
}
.cell {
  width: 2rem; height: 2rem;
  /* slightly different hue than surrounding for clear visibility */
  background: #162235; /* higher contrast vs body bg */
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.15rem;
  color: #fff;
}
/* Current ball: gentle hue shift, no border lines */
.cell.current { background: rgba(99, 102, 241, 0.18); }
/* Guidance state before user taps: soft tint */
.cell.guide { background: rgba(148, 163, 184, 0.12); }
.cell.pulse { animation: pulse 600ms ease; }
@keyframes pulse { 0% { transform: scale(0.9);} 60% { transform: scale(1.1);} 100% { transform: scale(1);} }

.pad.emotions {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  background: linear-gradient(180deg, rgba(11,15,20,0.3), var(--card));
  /* bottom padding includes safe area for iOS */
  padding: 12px 12px calc(16px + env(safe-area-inset-bottom, 0px));
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(72px, 1fr));
  gap: 8px;
  z-index: 20;
}
.pad.emotions { display: none; }
body.pad-visible .pad.emotions { display: grid; }
.emo {
  background: #0b1220;
  border: 1px solid #1f2a44;
  border-radius: 14px;
  padding: 12px 8px;
  font-size: clamp(20px, calc(var(--pad-tile) * 0.38), 36px);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--text);
}
.emo span { font-size: clamp(10px, calc(var(--pad-tile) * 0.18), 14px); color: var(--muted); margin-top: 6px; }
.emo:active { transform: translateY(1px); }
.emo.disabled { opacity: 0.5; pointer-events: none; }

@media (min-width: 640px) {
  .pad.emotions { grid-template-columns: repeat(auto-fit, minmax(72px, 1fr)); }
}

/* Summary */
.summary { margin-top: 16px; }
.summary h2 { font-size: 16px; color: var(--muted); margin: 8px 0; }
.strongest {
  background: #0f172a;
  border: 1px solid #1f2a44;
  border-radius: 12px;
  padding: 10px 12px;
  display: flex; align-items: center; gap: 8px;
  font-size: 14px;
}
.strongest .pill {
  background: #111827;
  border: 1px solid #293449;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 13px;
}
.moments { margin-top: 8px; display: grid; gap: 6px; }
.moment-row {
  display: flex; align-items: center; justify-content: space-between;
  background: #0f172a;
  border: 1px solid #1f2a44; border-radius: 10px;
  padding: 8px 10px;
}
.moment-row .left { display: flex; align-items: center; gap: 8px; }
.moment-row .label { color: var(--muted); font-size: 13px; }
.moment-row .emoji { font-size: 18px; }
.moment-row .total { font-size: 12px; color: var(--muted); }

/* New UI sections */
.hidden { display: none; }
.card { background: var(--card); border: 1px solid #1f2a44; border-radius: 12px; padding: 12px; margin-bottom: 12px; }
/* Avoid clipping inner content like arrows/popped emoji on small screens */
/* .card { overflow: hidden; }  -- removed */
.card > h2 { text-align: center; }
.row { display: flex; gap: 8px; align-items: center; margin-top: 8px; justify-content: center; }
.row.tight { gap: 10px; flex-wrap: nowrap; }
.input.sm { width: 160px; max-width: 45vw; }
@media (max-width: 420px) { .input.sm { width: 130px; } }
.grow { flex: 1; }
.input { flex: 1; background: #0b1220; color: var(--text); border: 1px solid #1f2a44; border-radius: 10px; padding: 10px 12px; font-size: 16px; }
/* Prevent iOS Safari input zoom by ensuring minimum 16px */
input, select, textarea { font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.room-header { display: flex; justify-content: space-between; align-items: center; font-size: 14px; color: var(--muted); position: relative; }
.room-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Icon button (kebab) */
.icon-btn {
  background: transparent; border: 1px solid #2a3a55; color: #b6c2d6;
  width: 36px; height: 32px; border-radius: 8px; font-size: 18px; line-height: 1;
}
.icon-btn:focus-visible { outline: 2px solid #93c5fd; outline-offset: 2px; }

/* Overflow menu */
.menu { position: absolute; top: 36px; right: 0; background: #0f172a; border: 1px solid #1f2a44; border-radius: 10px; padding: 6px; display: none; z-index: 50; min-width: 140px; box-shadow: 0 10px 24px rgba(0,0,0,0.35); }
.menu.open { display: block; }
.menu-item { width: 100%; text-align: left; background: transparent; border: 0; color: #e2e8f0; padding: 8px 10px; border-radius: 8px; font-size: 14px; }
.menu-item:hover, .menu-item:focus { background: #1e293b; }
.menu-item.danger { color: #fecaca; }
.menu-sep { height: 1px; background: #1f2a44; margin: 4px 2px; }
.stats { margin-top: 8px; font-size: 14px; color: var(--muted); }
.heat .cell { position: relative; }
.heat .cell[data-intensity="0"] { background: #162235; }
.heat .cell[data-intensity="1"] { background: #1f2937; }
.heat .cell[data-intensity="2"] { background: #374151; }
.heat .cell[data-intensity="3"] { background: #4b5563; }
.heat .cell[data-intensity="4"] { background: #6b7280; }
.heat .cell[data-intensity="5"] { background: #9ca3af; color: #111827; }

/* Lists */
.list { display: grid; gap: 8px; }
.list .item { display: flex; justify-content: space-between; align-items: center; background: #0f172a; border: 1px solid #1f2a44; border-radius: 10px; padding: 12px; }
.list .item .name { flex: 1; margin-right: 10px; }
.list .item .btn { margin-left: 8px; }
.list .item .actions { display: flex; gap: 6px; }
.admin { margin-top: 16px; }
.admin.locked .row:not(:first-of-type) { opacity: 0.5; pointer-events: none; }

/* Live feed area for interactive view */
.live-feed { height: 80px; margin-top: 8px; border-radius: 10px; display: flex; align-items: center; gap: 6px; padding: 8px; overflow-x: auto; background: radial-gradient(80% 120% at 50% 0%, rgba(99,102,241,0.10), rgba(15,23,42,0.5)); }
.live-feed.large { height: 140px; }
.burst { font-size: 28px; animation: pop 350ms ease-out; }
@keyframes pop { 0% { transform: scale(0.5); opacity: 0.3;} 80% { transform: scale(1.15); } 100% { transform: scale(1); opacity: 1;} }

/* Cumulative view panels */
.two-col { display: grid; grid-template-columns: 1fr; gap: 12px; }
.panel { background: #0f172a; border: 1px solid #1f2a44; border-radius: 10px; padding: 10px; }
.panel-title { color: var(--muted); font-size: 13px; margin-bottom: 6px; }

/* Mood Grid panels layout */
.mood-panels { display: grid; grid-template-columns: 1fr; gap: 12px; }

/* Axis labels wrapper */
.grid-block { display: grid; grid-template-columns: 1.6rem 1fr; grid-template-rows: auto 1fr; row-gap: var(--gap); }
.grid-block .grid-scroll { grid-column: 2; grid-row: 1 / span 2; overflow-x: auto; }
.grid-block .top-axis { display: grid; grid-template-columns: repeat(20, var(--cell)); gap: var(--gap); color: #93a0b4; font-size: 11px; }
.grid-block .top-axis span { text-align: center; }
.grid-block .left-axis { grid-column: 1; grid-row: 2; display: grid; grid-template-rows: repeat(6, var(--cell)); gap: var(--gap); color: #93a0b4; font-size: 11px; align-items: center; justify-items: center; }
.grid-block .timeline-grid { }

/* Tabs */
.tabs { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.tabs { justify-content: center; }
.tab { background: #1e293b; color: #e2e8f0; border: 1px solid #2a3a55; padding: 8px 12px; border-radius: 999px; font-size: 14px; }
.tab.active { background: #4f46e5; color: #fff; border-color: #5458df; }
.tab:focus-visible { outline: 2px solid #93c5fd; outline-offset: 2px; }
.tab-panel { margin-top: 6px; }
#panel-replay.tab-panel { margin-top: 12px; }

/* Replay grid */
.replay-wrap { position: relative; width: 100%; }
.replay-grid { /* repurposed as timeline container */
  display: block;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
  padding: 32px 0 20px; /* extra space so popped emoji and track aren't cut */
  min-height: 96px;
  position: relative;
  overscroll-behavior-x: contain;
  scrollbar-width: none; /* Firefox */
  touch-action: pan-x;
}
.replay-grid::-webkit-scrollbar { display: none; }
.replay-grid { cursor: grab; }
.replay-grid.dragging { cursor: grabbing; }
.replay-center { display: none; }
.replay-hairline { position: absolute; top: 12px; left: 50%; transform: translateX(-0.5px); width: 2px; height: 38px; background: #3b4861; opacity: 0.8; z-index: 2; pointer-events: none; border-radius: 1px; }
.replay-track { display: flex; flex-wrap: nowrap; gap: 24px; align-items: center; position: relative; }
.replay-track::before { content: ""; position: absolute; left: 0; right: 0; height: 2px; background: #1f2a44; top: 50%; transform: translateY(-50%); }
.replay-grid { position: relative; }
.track-spacer { flex: 0 0 auto; width: 0; height: 1px; }
.over-stop { position: relative; flex: 0 0 64px; width: 64px; scroll-snap-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; }
.over-stop .ov { font-size: 11px; color: #93a0b4; }
.over-stop .dot { width: 12px; height: 12px; border-radius: 50%; background: #1f2a44; box-shadow: 0 0 0 2px #0b1220; }
.over-stop[data-inn="1"] .dot[data-level="1"] { background: #2a3b5c; }
.over-stop[data-inn="1"] .dot[data-level="2"] { background: #36537f; }
.over-stop[data-inn="1"] .dot[data-level="3"] { background: #4a6692; }
.over-stop[data-inn="1"] .dot[data-level="4"] { background: #5b79a6; }
.over-stop[data-inn="1"] .dot[data-level="5"] { background: #6e8fbe; }
/* Innings II: distinct teal/greenish hues for clearer contrast */
.over-stop[data-inn="2"] .dot[data-level="1"] { background: #1f4d45; }
.over-stop[data-inn="2"] .dot[data-level="2"] { background: #1d6b5c; }
.over-stop[data-inn="2"] .dot[data-level="3"] { background: #1f846e; }
.over-stop[data-inn="2"] .dot[data-level="4"] { background: #24997e; }
.over-stop[data-inn="2"] .dot[data-level="5"] { background: #2fb08f; }
.over-stop .emo-pop { position: absolute; bottom: 22px; font-size: 22px; transform: translateY(8px) scale(0.8); opacity: 0; transition: transform 200ms ease, opacity 200ms ease; }
.over-stop.active .emo-pop { opacity: 1; transform: translateY(-4px) scale(1); animation: pop 350ms ease-out; }
.over-stop .tot { display: none; }
.over-stop.active .ov { visibility: hidden; }

/* Minimal popover for over details */
.replay-popover { display: none; }

/* Minimal info line below timeline */
.replay-info { text-align: center; margin-top: 4px; }
.replay-info .pill { display: inline-flex; align-items: center; gap: 8px; background: #0f172a; border: 1px solid #1f2a44; border-radius: 999px; padding: 6px 10px; color: #cbd5e1; }
.replay-info .sep { color: #64748b; }

/* Micro info under active stop */
.micro-info { display: none; margin-top: 4px; font-size: 11px; color: #93a0b4; background: #0f172a; border: 1px solid #1f2a44; border-radius: 999px; padding: 2px 6px; }
.over-stop.active .micro-info { display: inline-block; }

/* Bottom sheet for replay details */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: #0b1220; border-top: 1px solid #1f2a44;
  border-radius: 12px 12px 0 0; padding: 12px;
  display: none; z-index: 60;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
}
.sheet.open { display: block; }
.sheet .title { text-align: center; font-weight: 700; }
.sheet .sub { text-align: center; color: #93a0b4; font-size: 12px; }
.bars { margin-top: 10px; display: grid; gap: 6px; }
.bar { display: grid; grid-template-columns: 1fr auto; gap: 6px; align-items: center; }
.bar .lbl { font-size: 12px; color: #93a0b4; }
.bar .meter { height: 8px; background: #1f2a44; border-radius: 999px; overflow: hidden; }
.bar .fill { height: 100%; background: #4f46e5; }
.peak { margin-top: 8px; text-align: center; font-size: 12px; color: #93a0b4; }

@media (min-width: 800px) {
  .two-col { grid-template-columns: 1fr 1fr; }
}

/* Mobile tweaks for very small screens */
@media (max-width: 480px) {
  :root { --cell: 1.6rem; --gap: 4px; }
  .cell { width: 1.6rem; height: 1.6rem; font-size: 1rem; }
  .ball-label { font-size: 52px; }
  /* Extra breathing room for replay on phones */
  .replay-grid { padding: 40px 0 28px; min-height: 112px; }
  .replay-hairline { top: 16px; height: 46px; }
  .replay-track { gap: 16px; }
  .over-stop { flex: 0 0 56px; width: 56px; }
  .over-stop .ov { font-size: 10px; }
  .over-stop .dot { width: 10px; height: 10px; }
  .over-stop .emo-pop { font-size: 20px; }
  .topbar h1 { font-size: 16px; }
  .match-name { font-size: 12px; max-width: 60%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #global-title { font-size: 18px; }
  .replay-info .pill { padding: 5px 8px; }
}

/* Center text headings and tabs */
.card > h2, #global-title { text-align: center; }
.header-row { display: flex; align-items: center; justify-content: center; gap: 8px; position: relative; }
.header-row .icon-btn { position: absolute; right: 0; }
.header-row h2 { margin: 0; max-width: 100%; white-space: normal; overflow-wrap: anywhere; word-break: break-word; }
@media (max-width: 480px) {
  .header-row { padding: 0 36px; }
}
.header-row { margin-bottom: 10px; }
/* Slightly larger gap between header and tabs on Global/Replay */
#global-view .tabs { margin-top: 8px; }

/* Archived layout: place compact menu above, title below for more space */
#global-view.archived .header-row { display: grid; grid-template-columns: 1fr; grid-auto-rows: min-content; justify-items: center; gap: 6px; padding: 0 12px; }
#global-view.archived .header-row .icon-btn { position: static; justify-self: end; }
#global-view.archived #global-title { text-align: center; margin-top: 2px; }
.tabs { justify-content: center; }
.ball-label { position: relative; }
.ball-label sup.ball-sup { font-size: 0.45em; vertical-align: super; margin-left: 6px; color: #c7d2fe; }
.next-row { display: flex; justify-content: center; margin-top: 6px; margin-bottom: 6px; }
.btn.next { font-size: clamp(14px, 2.6vw, 18px); padding: 12px 18px; min-width: 220px; }
.segmented { display: inline-flex; align-items: center; gap: 6px; justify-self: center; }
.segmented .seg { background: #1e293b; color: #e2e8f0; border: 1px solid #2a3a55; padding: 4px 10px; border-radius: 999px; font-size: 13px; }
.segmented .seg.active { background: #4f46e5; color: #fff; border-color: #5458df; }
.segmented .seg:focus-visible { outline: 2px solid #93c5fd; outline-offset: 2px; }
.innings-chip { display: none; }
.mood-grid-header { display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; }
.replay-nudge { position: absolute; top: 50%; transform: translateY(-50%); width: 28px; height: 28px; border-radius: 50%; border: 1px solid #1f2a44; background: rgba(15,18,32,0.85); color: #cbd5e1; display: none; align-items: center; justify-content: center; z-index: 3; }
.replay-nudge.left { left: 6px; }
.replay-nudge.right { right: 6px; }
.replay-nudge:active { transform: scale(0.98); }
.inn-sep { flex: 0 0 auto; width: 10px; height: 36px; position: relative; background: transparent; }
.inn-sep.double::before,
.inn-sep.double::after { content: ""; position: absolute; top: 0; width: 3px; height: 100%; background: #475569; border-radius: 2px; }
.inn-sep.double::before { left: 0; }
.inn-sep.double::after { right: 0; }

/* Ensure global view fully fits viewport on small screens */
@media (max-width: 480px) {
  main.container { max-width: 100vw; }
  #global-view, #panel-replay, .card { width: 100%; max-width: 100%; box-sizing: border-box; }
}
