/* ---------- Farben (Tokens) ---------- */
:root {
  --bg: #f2f2f7;
  --bar: rgba(249, 249, 251, 0.86);
  --surface: #ffffff;
  --seg: rgba(118, 118, 128, 0.12);
  --text: #1c1c1e;
  --muted: #6e6e73;
  --sep: rgba(60, 60, 67, 0.2);
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.12);
  --court: #3f8f5a;
  --court-line: rgba(255, 255, 255, 0.85);
  --crease: rgba(255, 255, 255, 0.12);
  --team-a: #0a84ff;
  --team-b: #ff453a;
  --team-g: #ffd60a;
  --cone: #ff9f0a;
  --pass: #ffd60a;
  --run: #ffffff;
  --board: #7d91a6;
  --board-edge: #3f5263;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000;
    --bar: rgba(28, 28, 30, 0.86);
    --surface: #1c1c1e;
    --seg: rgba(118, 118, 128, 0.26);
    --text: #f2f2f7;
    --muted: #98989f;
    --sep: rgba(84, 84, 88, 0.65);
    --accent: #0a84ff;
    --accent-soft: rgba(10, 132, 255, 0.22);
    --court: #2d6b47;
    --court-line: rgba(255, 255, 255, 0.8);
    --crease: rgba(255, 255, 255, 0.08);
    --board: #4b5b6c;
    --board-edge: #2c3947;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  --bg: #000000;
  --bar: rgba(28, 28, 30, 0.86);
  --surface: #1c1c1e;
  --seg: rgba(118, 118, 128, 0.26);
  --text: #f2f2f7;
  --muted: #98989f;
  --sep: rgba(84, 84, 88, 0.65);
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.22);
  --court: #2d6b47;
  --court-line: rgba(255, 255, 255, 0.8);
  --crease: rgba(255, 255, 255, 0.08);
  --board: #4b5b6c;
  --board-edge: #2c3947;
  --shadow: none;
}

[hidden] { display: none !important; }

/* ---------- Grundlayout: App-Vollbild ohne Seitenscroll ---------- */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.3 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

button, input {
  font: inherit;
  color: inherit;
}

button {
  border: 0;
  background: none;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled { opacity: 0.35; cursor: default; }

.app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "top"
    "main"
    "tools";
}

/* ---------- Topbar ---------- */
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--bar);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--sep);
}

.title-input {
  flex: 1;
  min-width: 0;
  padding: 8px;
  border: 0;
  outline: none;
  background: transparent;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
  text-overflow: ellipsis;
}

.title-input:focus { background: var(--seg); border-radius: 10px; }

.icon-btn {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 20px;
  background: var(--seg);
}

.btn {
  flex: none;
  min-height: 36px;
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--seg);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.btn.primary { background: var(--accent); color: #fff; }

.chip, .btn, .icon-btn, .seg button { transition: transform 0.12s ease, opacity 0.12s ease; }
.chip:active, .btn:active, .icon-btn:active, .seg button:active { transform: scale(0.96); opacity: 0.75; }

/* ---------- Stage (Spielfeld + Frame-Leiste) ---------- */
.stage {
  grid-area: main;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.court-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
}

#court {
  position: absolute;
  inset: 10px 14px;
  width: calc(100% - 28px);
  height: calc(100% - 20px);
  touch-action: none;
  overflow: hidden;
}

#court[data-tool="run"], #court[data-tool="pass"] { cursor: crosshair; }
#court[data-tool="erase"] { cursor: pointer; }
#court[data-tool="label"] { cursor: text; }

.view-bar {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  gap: 8px;
  padding: 8px 12px 0;
  min-width: 0;
}

.view-bar .seg { flex: none; }
.view-bar .s { display: inline; }

@media (max-width: 600px) {
  .view-bar { gap: 6px; }
  .view-bar .seg button { padding: 6px 7px; font-size: 13px; }
  .view-bar .s { display: none; }
}

/* Linien: Schuss (Doppellinie) */
.stroke.shotline { stroke: var(--run); stroke-width: 0.1; }
.draft * { pointer-events: none; }

/* Zonen */
.zone-fill { stroke-width: 0.1; stroke-linejoin: round; }
.zone-item.sel .zone-fill { stroke-dasharray: 0.4 0.3; }
.handle { fill: #fff; stroke: var(--accent); stroke-width: 0.12; cursor: nwse-resize; }

/* Popovers (Farben, Zone) über der Frame-Leiste */
.popover {
  position: absolute;
  left: 50%;
  bottom: 64px;
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px 14px;
  max-width: calc(100% - 24px);
  padding: 12px 14px;
  border-radius: 16px;
  background: var(--surface);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
}
.popover[hidden] { display: none; }
.color-field { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 500; white-space: nowrap; }
.color-field input[type="color"] { width: 36px; height: 30px; border: 0; padding: 0; background: none; cursor: pointer; }
.color-field input[type="number"] { width: 64px; min-height: 32px; border-radius: 8px; border: 0; padding: 4px 8px; background: var(--seg); color: var(--text); font: inherit; }
.color-field input[type="range"] { width: 110px; accent-color: var(--accent); }
.zone-panel .seg button { min-height: 34px; }

.net { fill: rgba(255, 255, 255, 0.14); }
.mesh { fill: none; stroke: rgba(255, 255, 255, 0.35); stroke-width: 0.03; }
.goalframe { fill: none; stroke: #ff453a; stroke-width: 0.16; stroke-linejoin: round; stroke-linecap: round; }
.app.playing .view-bar { opacity: 0.45; pointer-events: none; }

.floor { fill: var(--court); }
.ring { fill: none; stroke: var(--board); stroke-width: 1; }
.mark { fill: none; stroke: var(--court-line); stroke-width: 0.14; }
.goal { fill: rgba(128, 128, 128, 0.35); stroke: #ff453a; stroke-width: 0.12; }
.zone { stroke: var(--court-line); stroke-width: 0.3; stroke-linecap: butt; }
.board { fill: var(--board); stroke: var(--board-edge); stroke-width: 0.05; stroke-linejoin: round; }

.arrow-run { fill: var(--run); }
.arrow-pass { fill: var(--pass); }

.stroke {
  fill: none;
  stroke-width: 0.26;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}
.stroke.run { stroke: var(--run); }
.stroke.pass { stroke: var(--pass); stroke-dasharray: 0.6 0.45; }
.draft { opacity: 0.7; }

.hit-line {
  fill: none;
  stroke: transparent;
  stroke-width: 1.1;
  stroke-linecap: round;
  pointer-events: stroke;
}

.item {
  transition: transform 0.9s cubic-bezier(0.45, 0, 0.2, 1);
  cursor: grab;
}
.item.dragging { transition: none; cursor: grabbing; }

.item .hit { fill: transparent; }
.pl { stroke: #fff; stroke-width: 0.1; }
.pl.team-a { fill: var(--team-a); }
.pl.team-b { fill: var(--team-b); }
.pl.team-g { fill: var(--team-g); }
.ball { fill: #fff; stroke: #1c1c1e; stroke-width: 0.08; }
.cone { fill: var(--cone); }

.pl-label {
  fill: #fff;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}
.pl-label.dark { fill: #1c1c1e; }

.frame-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px 12px 8px;
}

.frame-label {
  min-width: 96px;
  text-align: center;
  color: var(--muted);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ---------- Werkzeugleiste (unten) ---------- */
.toolbar {
  grid-area: tools;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px 12px;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
  background: var(--bar);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--sep);
}

.app.playing .toolbar,
.app.playing .frame-bar button:not(#playBtn) { opacity: 0.45; pointer-events: none; }

.seg {
  flex: none;
  display: flex;
  padding: 2px;
  border-radius: 10px;
  background: var(--seg);
}

.seg button {
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
}

.seg button[aria-pressed="true"] {
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18);
  font-weight: 600;
}

.add-group {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.chip {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
}

.dot { flex: none; }
.chip .dot { width: 12px; height: 12px; border-radius: 50%; }
.dot.team-a { background: var(--team-a); }
.dot.team-b { background: var(--team-b); }
.dot.team-g { background: var(--team-g); }
.dot.ball-dot { background: #fff; box-shadow: inset 0 0 0 1.5px #1c1c1e; }
.dot.cone-dot { background: var(--cone); border-radius: 2px; transform: rotate(45deg) scale(0.8); }

/* ---------- Taktikliste (Seitenleiste / Drawer) ---------- */
.sidebar {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 40;
  width: min(320px, 84vw);
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  background: var(--surface);
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.25);
  transform: translateX(-105%);
  transition: transform 0.32s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.app.side-open .sidebar { transform: none; }

.scrim {
  position: fixed;
  inset: 0;
  z-index: 35;
  background: rgba(0, 0, 0, 0.3);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}
.app.side-open .scrim { opacity: 1; pointer-events: auto; }

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 16px 16px 8px;
}

.sidebar-head h1 { margin: 0; font-size: 26px; font-weight: 700; letter-spacing: -0.3px; }

.play-list {
  flex: 1;
  margin: 0;
  padding: 4px 8px 16px;
  list-style: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.play-item {
  display: flex;
  align-items: center;
  border-radius: 12px;
}
.play-item.active { background: var(--accent-soft); }

.play-open {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  text-align: left;
}

.play-name, .play-meta { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.play-name { font-weight: 600; }
.play-meta { font-size: 12px; color: var(--muted); }

.sidebar-foot {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 0.5px solid var(--sep);
}
.sidebar-foot .btn { flex: 1; min-width: 110px; }

.play-del {
  flex: none;
  width: 36px;
  height: 36px;
  margin-right: 4px;
  border-radius: 50%;
  color: var(--muted);
}

/* ---------- Wide: iPad Landschaft & Desktop ---------- */
@media (min-width: 900px) {
  .app {
    grid-template-columns: 300px minmax(0, 1fr);
    grid-template-areas:
      "side top"
      "side main"
      "side tools";
  }

  .sidebar {
    position: relative;
    grid-area: side;
    width: auto;
    padding-top: 0;
    transform: none;
    box-shadow: none;
    border-right: 0.5px solid var(--sep);
  }

  .scrim, .menu-btn { display: none; }
}

@media (max-width: 600px) {
  .chip { padding: 8px 10px; min-height: 36px; }
  .seg button { padding: 6px 9px; }
  .frame-label { min-width: 0; }
}

@media (min-width: 900px) and (orientation: landscape) {
  .title-input { font-size: 19px; }
}

/* ---------- Präsentieren: Feld gross, nur Abspielen & Frames ---------- */
.app.presenting {
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas:
    "top"
    "main";
}
.app.presenting .sidebar,
.app.presenting .scrim,
.app.presenting .toolbar,
.app.presenting .view-bar > .seg,
.app.presenting #colorsBtn,
.app.presenting #undoBtn { display: none; }
.present-tools { display: none; align-items: center; gap: 8px; }
.app.presenting .present-tools { display: flex; }
.present-tools .color-field input[type="color"] { width: 44px; }
#court[data-tool="paint"] { cursor: crosshair; }
.app.presenting #court { inset: 4px 6px; width: calc(100% - 12px); height: calc(100% - 8px); }
.app.presenting .view-bar { padding-top: 6px; }
.app.presenting .frame-bar { padding-bottom: calc(env(safe-area-inset-bottom) + 10px); }

@media (prefers-reduced-motion: reduce) {
  .item, .sidebar, .scrim { transition: none; }
}
