/* Preparation and recording share the existing controls and data model. */
#analysis-view [hidden],
#data-view [hidden],
.alert-badge[hidden] {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.analysis-entry {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.analysis-entry h2,
.analysis-entry p { margin: 0; }
.analysis-entry p { margin-top: 4px; }
:root {
  --topbar-height: 100px;
  --analysis-scorebar-height: 120px;
  scroll-padding-top: calc(var(--topbar-height) + 16px);
}

#analysis-stage-title { scroll-margin-top: 0; }

#analysis-dashboard .analysis-summary-title { margin-bottom: 12px; }
#analysis-dashboard .finish-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: end;
  gap: 10px;
  margin-bottom: 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
}
#analysis-dashboard .finish-controls label { min-width: 0; }
#analysis-dashboard .finish-controls select { width: 100%; min-width: 0; }
#analysis-dashboard #finish-match { grid-column: 1 / -1; }

.analysis-saved-panel,
.data-diagnostics {
  padding: 12px 16px;
  margin-bottom: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.analysis-saved-panel > summary,
.setup-more > summary,
.fighter-more > summary,
.analysis-history > summary,
.data-diagnostics > summary {
  cursor: pointer;
  padding: 8px 0;
  font-weight: 700;
}

#analysis-view .saved-match-bar {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr) auto;
  margin-top: 8px;
}

#analysis-view .setup-fields {
  border: 0;
  padding: 0;
  margin: 0;
  min-width: 0;
}

#analysis-view .setup-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
#analysis-view .setup-more { margin-top: 8px; }
#analysis-view .setup-more[open] > .setup-grid { margin-top: 8px; }
#analysis-view .fighter-form { gap: 8px; }

.fighter-identity {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
}

#analysis-view .competitor-photo-control {
  width: 56px;
  height: 64px;
  min-height: 0;
  margin: 0;
}

.fighter-basic-fields,
.fighter-detail-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.fighter-more[open] .fighter-detail-fields { margin: 8px 0; }
#analysis-view .fighter-more .photo-help { display: block; margin-top: 8px; }
#analysis-view .setup-actions { margin: 10px 0 16px; align-items: end; }
#analysis-view .setup-video-link { min-width: min(100%, 340px); flex: 1 1 340px; }
#analysis-view .setup-video-link input { width: 100%; min-width: 0; }
#analysis-view .setup-actions #new-match { margin-left: auto; }
#active-setup-note { margin-bottom: 8px; }
#edit-active-match { margin-bottom: 12px; }
#analysis-view .setup-fields:disabled input,
#analysis-view .setup-fields:disabled select { color: var(--muted); background: #f6f7f8; }

.data-record-counts { overflow-x: auto; margin: 16px 0 8px; }
.data-record-counts table { width: 100%; border-collapse: collapse; }
.data-record-counts caption { text-align: left; font-weight: 700; margin-bottom: 8px; }
.data-record-counts th,
.data-record-counts td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; }
.data-record-counts th[scope="row"] { text-transform: capitalize; }
.data-diagnostics { margin-top: 12px; }
.data-diagnostics pre { max-height: 360px; overflow: auto; }

@media (min-width: 981px) {
  .topbar { gap: 16px; padding: 12px 18px; }
  .topbar .brand-block { flex: 0 1 320px; min-width: 0; }
  .topbar .club-logo { width: 48px; height: 48px; flex-shrink: 0; }
  .topbar h1 { font-size: clamp(22px, 2vw, 32px); }
  .topbar .tabs {
    flex: 1;
    min-width: 0;
    flex-wrap: nowrap;
    overflow-x: auto;
    align-items: center;
    gap: 6px;
  }
  .topbar .tab,
  .topbar .logout-button { flex: 0 0 auto; min-height: 40px; padding: 0 10px; font-size: 13px; white-space: nowrap; }

  #analysis-view .competitor-status-bar {
    top: calc(var(--topbar-height) + 12px);
    z-index: 5;
  }
  #analysis-view .workbench.video-mode .control-panel {
    top: calc(var(--topbar-height) + var(--analysis-scorebar-height) + 24px);
    max-height: calc(100vh - var(--topbar-height) - var(--analysis-scorebar-height) - 40px);
  }

  #analysis-view .workbench:not(.video-mode) {
    grid-template-columns: minmax(0, 1fr) minmax(330px, 410px);
    grid-template-rows: auto 1fr;
  }
  #analysis-view .workbench:not(.video-mode) .competitor-status-bar {
    grid-column: 1;
    grid-row: 1;
    position: static;
  }
  #analysis-view .workbench:not(.video-mode) .side-panel { grid-template-columns: 1fr; }
  #analysis-view .workbench:not(.video-mode) .video-panel { grid-column: 1; grid-row: 2; }
  #analysis-view .workbench:not(.video-mode) .control-panel {
    grid-column: 2;
    grid-row: 1 / 3;
    position: sticky;
    top: calc(var(--topbar-height) + 12px);
    max-height: calc(100vh - var(--topbar-height) - 28px);
  }
  #analysis-view .workbench:not(.video-mode) .timer-strip { grid-template-columns: auto auto minmax(0, 1fr); }
}

@media (min-width: 981px) and (max-width: 1440px) {
  .topbar { gap: 12px; padding: 10px 14px; }
  .topbar .brand-block { flex-basis: 270px; gap: 8px; }
  .topbar .club-logo { width: 40px; height: 40px; }
  .topbar h1 { font-size: 22px; white-space: nowrap; }
  .topbar .project-credit { font-size: 10px; line-height: 1.3; }
  .topbar .tabs { gap: 4px; }
  .topbar .tab,
  .topbar .logout-button { padding: 0 8px; font-size: 12px; }
}

@media (max-width: 760px) {
  #analysis-view .setup-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #analysis-view .saved-match-bar { grid-template-columns: 1fr; }
  #analysis-view .setup-summary-toggle { grid-template-columns: 1fr auto; }
  #analysis-view .setup-summary-toggle > span { grid-column: 1 / -1; }
  #analysis-view .setup-summary-toggle strong { white-space: normal; }
}

@media (max-width: 520px) {
  #analysis-dashboard .finish-controls { grid-template-columns: 1fr; }
  #analysis-view .setup-grid,
  #analysis-view .fighters-grid { grid-template-columns: 1fr; }
  .analysis-entry { align-items: flex-start; }
}

@media print {
  .analysis-entry,
  .analysis-saved-panel { display: none; }
}

/* Keep the live scorebar compact as more actions are recorded. */
@media (min-width: 981px) {
  #analysis-view .competitor-status-bar .quick-action-log {
    height: clamp(168px, 18vh, 196px);
    max-height: clamp(168px, 18vh, 196px);
    min-height: 0;
    grid-template-rows: auto minmax(0, 1fr);
    overflow: hidden;
  }

  #analysis-view .competitor-status-bar .quick-action-log > div {
    min-height: 0;
    grid-auto-rows: max-content;
    align-content: start;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }
}

/* The loaded video stays directly below the timer; the source picker can be reopened. */
#video-panel:not(.video-mode) .video-source-picker,
.video-source-picker:not([open]) .video-source-fields {
  display: none !important;
}

.video-source-picker > summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  list-style: none;
  padding: 8px 12px;
}

.video-source-picker > summary::-webkit-details-marker { display: none; }

#video-panel:not(:has(#fight-video.has-video, #youtube-player.has-youtube))
  .video-source-picker > summary {
  display: none;
}

@media (min-width: 981px) {
  #video-panel.video-mode:has(#fight-video.has-video, #youtube-player.has-youtube)
    .video-source-picker > summary {
    position: static;
    width: fit-content;
    margin: 5px 10px 5px auto;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: white;
    padding: 5px 8px;
  }

  #analysis-view .competitor-status-bar .quick-action-log {
    height: clamp(152px, 16vh, 176px);
    max-height: clamp(152px, 16vh, 176px);
    gap: 4px;
    padding: 6px;
  }

  #analysis-view .competitor-status-bar .score-side {
    padding: 8px 10px;
    row-gap: 2px;
  }

  #analysis-view .competitor-status-bar .score-side strong { font-size: 38px; }

  #analysis-view .competitor-status-bar .quick-action-item {
    grid-template-columns: auto minmax(0, 1fr);
    gap: 1px 7px;
    padding: 5px 7px;
  }

  #analysis-view .competitor-status-bar .quick-action-item span {
    grid-column: 1;
    grid-row: 1;
    white-space: nowrap;
  }

  #analysis-view .competitor-status-bar .quick-action-item strong {
    grid-column: 2;
    grid-row: 1;
  }

  #analysis-view .competitor-status-bar .quick-action-item small {
    grid-column: 2;
    grid-row: 2;
  }
}

/* Saved analyses use the same neutral, compact controls as the workbench. */
#analysis-view .analysis-library {
  margin: 12px 0; padding: 12px 14px; min-width: 0;
  border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
}
#analysis-view .analysis-library-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px 16px; flex-wrap: wrap;
}
#analysis-view .analysis-library-header h3 {
  margin: 0; color: var(--ink); font-size: 14px; font-weight: 700;
}
#analysis-view .analysis-status-filters {
  display: inline-flex; gap: 3px; padding: 3px; max-width: 100%;
  border: 1px solid var(--line); border-radius: 7px; background: var(--paper);
}
#analysis-view .analysis-status-filters button {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 34px; padding: 5px 10px; border: 0; border-radius: 5px;
  background: transparent; color: var(--muted); font: inherit; font-size: 12px;
  font-weight: 700; line-height: 1.3; white-space: nowrap; box-shadow: none;
}
#analysis-view .analysis-status-filters button:hover { background: #eeefef; color: var(--ink); }
#analysis-view .analysis-status-filters button.is-active { background: var(--ink); color: white; }
#analysis-view .analysis-status-count {
  display: inline-grid; place-items: center; min-width: 20px; padding: 2px 5px;
  border-radius: 4px; font-size: 11px; line-height: 1.2; background: #e7e9ec; color: var(--muted);
}
#analysis-view .is-active .analysis-status-count { background: #ffffff26; color: white; }
#analysis-view .analysis-status-filters button:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }
#analysis-view .analysis-library #saved-matches-panel,
#analysis-view .analysis-library #saved-matches-panel[open] { margin: 8px 0 0; padding: 0; border: 0; border-radius: 0; box-shadow: none; }
#analysis-view .analysis-library #saved-matches-panel > summary {
  width: fit-content; min-height: 30px; margin: 0; padding: 4px 0;
  border: 0; border-radius: 0; background: transparent; color: var(--muted); font-size: 12px; font-weight: 600;
}
#analysis-view .analysis-library .saved-match-bar { margin-top: 6px; padding-top: 10px; gap: 10px; }
#analysis-view .analysis-library .saved-match-bar label { min-width: 0; font-size: 12px; gap: 5px; }
#analysis-view .analysis-library .saved-match-bar input,
#analysis-view .analysis-library .saved-match-bar select,
#analysis-view .analysis-library #load-match { min-width: 0; min-height: 38px; font-size: 13px; padding: 8px 10px; }
#analysis-view .analysis-library .saved-match-bar select { width: 100%; text-overflow: ellipsis; }
@media (max-width: 600px) {
  #analysis-view .analysis-library { padding: 12px; }
  #analysis-view .analysis-library-header { align-items: stretch; }
  #analysis-view .analysis-status-filters { display: flex; width: 100%; }
  #analysis-view .analysis-status-filters button { flex: 1; min-height: 44px; padding: 6px; font-size: 11px; gap: 5px; }
  #analysis-view .analysis-library .saved-match-bar input,
  #analysis-view .analysis-library .saved-match-bar select,
  #analysis-view .analysis-library #load-match { min-height: 44px; }
}
@media print { #analysis-view .analysis-library { display: none; } }

/* Keep manual-save feedback visible and separate from the action editor. */
.analysis-sync-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
}
#analysis-sync-status {
  display: block;
  flex-basis: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-weight: 600;
  line-height: 1.5;
  color: #243342;
  background: #eef3f8;
  overflow-wrap: anywhere;
}
#analysis-sync-status[data-state="busy"] { color: #704400; background: #fff4d6; border-color: #c58b21; }
#analysis-sync-status[data-state="success"] { color: #14532d; background: #ecfdf3; border-color: #308052; }
#analysis-sync-status[data-state="error"] { color: #991b1b; background: #fff1f2; border-color: #cc4b58; }
@media (max-width: 600px) {
  .analysis-sync-toolbar button { flex: 1 1 100%; white-space: normal; }
}
