:root {
  color-scheme: dark;
  --bg: #0b1020;
  --card: #121a2b;
  --line: #e8ecf5;
  --court-fill: #17233d;
  --paint-fill: #1c2c4d;
  --text: #f6f7fb;
  --muted: #91a0b8;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

.topbar {
  padding: 16px 24px;
  border-bottom: 1px solid #1e2740;
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
}

.court-wrap {
  display: flex;
  justify-content: center;
  padding: 24px;
}

#courtCanvas {
  width: 100%;
  max-width: 720px;
  aspect-ratio: 4 / 3.76;
  background: var(--card);
  border-radius: 12px;
  touch-action: none;
}

.tray {
  display: flex;
  align-items: center;
  gap: 14px;
  justify-content: center;
  flex-wrap: wrap;
  padding: 16px 24px 0;
}

.tray-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  border-radius: 10px;
  background: #1e2740;
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.tray-chip:active {
  cursor: grabbing;
}

.tray-chip.disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.tray-chip-icon {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 18px;
  color: #fff;
}

.tray-chip-offense {
  background: #3b82f6;
}

.tray-chip-defense {
  background: transparent;
  color: #ff6b6b;
  border: 3px solid #ff6b6b;
}

.tray-chip-ball {
  background: #f5a623;
  width: 22px;
  height: 22px;
  align-self: center;
  margin: 6px 0;
}

.tray-chip-count {
  font-size: 11px;
  color: var(--muted);
}

.tray button.secondary {
  background: #1e2740;
  color: var(--muted);
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.tray button.secondary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.tool-palette {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  padding: 12px 24px 0;
}

.tool-btn {
  background: #1e2740;
  color: var(--text);
  border: 2px solid transparent;
  border-radius: 8px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.tool-btn.active {
  border-color: #3b82f6;
  background: #24325a;
}

.frame-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  padding: 0 24px 12px;
}

.frame-bar button.secondary {
  background: #1e2740;
  color: var(--muted);
  border: none;
  border-radius: 8px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.frame-bar button.secondary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.frame-label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  min-width: 100px;
  text-align: center;
}

.app-layout {
  display: flex;
  align-items: stretch;
  gap: 0;
  padding: 0 24px 24px;
  min-height: calc(100vh - 65px);
}

.main-panel {
  flex: 1 1 auto;
  min-width: 0;
}

.main-panel .court-wrap {
  padding: 24px 0;
}

.main-panel .tray,
.main-panel .tool-palette,
.main-panel .frame-bar {
  padding-left: 0;
  padding-right: 0;
}

.sidebar {
  flex: 0 0 auto;
  width: 240px;
  margin-right: 8px;
  background: var(--card);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
  transition: width 0.15s ease, margin 0.15s ease, opacity 0.15s ease;
  /* Bounded (not just stretched) to the viewport and pinned in place, so
     the sidebar never grows past what's visible — without this, only a
     min-height cascaded down from .app-layout, so a tall main panel made
     the whole sidebar (and its Frames tab) grow along with the page
     instead of scrolling its own content internally. */
  position: sticky;
  top: 16px;
  max-height: calc(100vh - 32px);
}

/* Suppressed while actively dragging the resize handle, so the width
   tracks the pointer 1:1 instead of lagging behind/animating toward it —
   the transition is only meant to smooth the collapse/expand toggle. */
.sidebar.resizing {
  transition: none;
}

/* Collapsing shrinks the sidebar to nothing rather than unmounting it, so
   its scroll position/DOM state (tree expansion, etc.) survives being
   hidden and shown again. Width is forced past the resize-set inline
   style with !important since that inline style is what drives the
   normal (non-collapsed) width. */
.sidebar.collapsed {
  width: 0 !important;
  margin-right: 0;
  padding: 0;
  opacity: 0;
  pointer-events: none;
}

.sidebar-toggle {
  flex: 0 0 auto;
  align-self: flex-start;
  position: sticky;
  top: 16px;
  width: 18px;
  height: 56px;
  margin-right: 8px;
  background: #1e2740;
  color: var(--muted);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
}

.sidebar-toggle:hover {
  background: #26335c;
  color: var(--text);
}

/* Sits between the sidebar and the toggle button; dragging it changes
   the sidebar's width. Hidden (rather than merely inert) while the
   sidebar is collapsed, since there's nothing to resize at that point. */
.sidebar-resize-handle {
  flex: 0 0 6px;
  align-self: flex-start;
  position: sticky;
  top: 16px;
  height: calc(100vh - 32px);
  margin-right: 12px;
  border-radius: 3px;
  cursor: col-resize;
  background: transparent;
}

.sidebar-resize-handle:hover,
.sidebar-resize-handle.dragging {
  background: #2c3a63;
}

.sidebar-resize-handle.hidden {
  display: none;
}

/* Switches between the Plays and Frames panels below the top-of-sidebar
   action buttons (currently just Export Image). Only one panel is shown
   at a time; each keeps its own scroll/content untouched while hidden,
   same rationale as the sidebar's own collapse (width: 0 rather than
   unmounting) for the tree state. Styled as flat underline tabs (not
   pill buttons) so they read as a view switcher, not another action. */
.sidebar-tabs {
  display: flex;
  gap: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-tab {
  background: none;
  color: var(--muted);
  border: none;
  border-radius: 0;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  padding: 6px 2px 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.sidebar-tab:hover {
  color: var(--text);
}

.sidebar-tab.active {
  color: var(--text);
  border-bottom-color: #3b82f6;
}

.sidebar-tab-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 1 1 auto;
  min-height: 0;
}

.sidebar-tab-panel.hidden {
  display: none;
}

.frames-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 60px;
}

.frame-thumb {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: #1e2740;
  border: 2px solid transparent;
  border-radius: 8px;
  padding: 6px;
  position: relative;
}

.frame-thumb:hover {
  border-color: rgba(255, 255, 255, 0.15);
}

.frame-thumb.current {
  border-color: #3b82f6;
  background: #24304f;
}

.frame-thumb-select {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-align: left;
  width: 100%;
}

.frame-thumb-canvas {
  width: 100%;
  height: auto;
  border-radius: 4px;
  display: block;
}

.frame-thumb-label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
}

.frame-thumb.current .frame-thumb-label {
  color: var(--text);
}

.frame-thumb-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 2px;
}

.frame-thumb-duplicate-btn,
.frame-thumb-delete-btn {
  background: rgba(11, 15, 26, 0.75);
  border: none;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  padding: 3px 6px;
  border-radius: 4px;
}

.frame-thumb-duplicate-btn:hover {
  background: #3b82f6;
}

.frame-thumb-delete-btn:hover {
  background: #ff6b6b;
}

.frame-thumb-delete-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.sidebar-title {
  margin: 0;
  font-size: 15px;
}

.sidebar-hint {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.plays-save-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.sidebar input {
  background: #1e2740;
  color: var(--text);
  border: 2px solid transparent;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  font-family: inherit;
}

.sidebar .plays-save-row input {
  flex: 1;
  min-width: 0;
}

.plays-toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.plays-toolbar-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  flex: 0 0 auto;
}

.plays-location {
  color: var(--muted);
  font-size: 11px;
  line-height: 1.3;
  overflow-wrap: anywhere;
  /* Reset button defaults so this reads like the plain label it used to
     be, aside from the pointer cursor hinting it's clickable. */
  background: none;
  border: none;
  padding: 0;
  text-align: left;
  cursor: pointer;
  flex: 1 1 auto;
  min-width: 0;
}

.plays-location:hover,
.plays-location:focus-visible {
  color: var(--text);
  text-decoration: underline;
}

.plays-tree {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 60px;
}

.plays-tree ul.plays-tree-children {
  list-style: none;
  margin: 0;
  /* Indents each nesting level relative to its parent folder; since this
     rule applies to every nested <ul>, deeper levels stack the indent
     cumulatively (e.g. a play two folders deep sits 28px in). The left
     border gives a faint vertical guide tying children back to their
     folder, on top of the indent itself. */
  padding: 0 0 0 16px;
  border-left: 1px solid #33406b;
  margin-left: 6px;
}

.plays-tree-row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 4px;
  border-radius: 6px;
}

.plays-tree-row.current {
  background: #24325a;
}

.plays-tree-row .toggle-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  padding: 0 2px;
  width: 14px;
  flex: 0 0 auto;
}

.plays-tree-row .folder-name,
.plays-tree-row .play-name {
  background: none;
  border: none;
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  text-align: left;
  cursor: pointer;
  padding: 2px 0;
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.plays-tree-row .folder-name::before {
  content: "📁 ";
}

.plays-tree-row .play-name::before {
  content: "▸ ";
  color: var(--muted);
}

.plays-tree-row .add-subfolder-btn,
.plays-tree-row .export-btn,
.plays-tree-row .delete-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  padding: 2px 4px;
  flex: 0 0 auto;
}

.plays-tree-row .export-btn:hover {
  color: #3b82f6;
}

.plays-tree-row .delete-btn:hover {
  color: #ff6b6b;
}

.plays-tree-row .rename-input {
  flex: 1 1 auto;
  min-width: 0;
  background: #1e2740;
  color: var(--text);
  border: 2px solid #3b82f6;
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 13px;
  font-family: inherit;
}

.plays-tree-empty {
  color: var(--muted);
  font-size: 12px;
  padding: 4px;
}

.sidebar button:not(.sidebar-tab):not(.frame-thumb-select):not(.frame-thumb-duplicate-btn):not(.frame-thumb-delete-btn) {
  border: none;
  border-radius: 8px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.sidebar button:not(.secondary):not(.sidebar-tab):not(.frame-thumb-select):not(.frame-thumb-duplicate-btn):not(.frame-thumb-delete-btn) {
  background: #3b82f6;
  color: #fff;
}

.sidebar button.secondary {
  background: #1e2740;
  color: var(--muted);
}

.sidebar-export-btn {
  width: 100%;
  padding: 8px 14px;
}

.sidebar-actions {
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.sidebar button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.hint {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  margin: 0 0 16px;
}
