/* ── Map history mode + war advisor (/map) ──
   Layout only: the history panel sits below the Leaflet container inside .map-stack, and the
   war advisor floats clear of the sticky navigation (z-index 3000). */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.map-stack {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

/* Overrides the archive floor so the panel and the map share one viewport height. */
.map-stack #map {
  flex: 1 1 auto;
  min-height: 260px;
  height: auto;
}

.history-panel[hidden] {
  display: none !important;
}

/* The live sidebar slides over the right side (360px); reserve that width while it is open so
   the event list stays reachable. Below 768px the sidebar becomes a bottom sheet instead. */
.page-inner:has(> .sidebar.open) .history-panel {
  padding-right: calc(var(--sidebar-w) + 14px);
}

.history-panel {
  flex: 0 1 auto;
  min-height: 0;
  max-height: 42vh;
  overflow-y: auto;
  padding: 12px 14px 14px;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(195, 154, 80, 0.28);
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-family: var(--font-body);
}

.history-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text2);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}
.history-toggle:hover {
  border-color: var(--gold-dark);
  color: var(--text);
}
.history-toggle:has(input:checked) {
  border-color: var(--gold);
  color: var(--gold-light);
  background: color-mix(in srgb, var(--gold) 14%, var(--surface));
}
.history-toggle input {
  accent-color: var(--gold);
  margin: 0;
}

.mh-query {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px 12px;
  margin: 0;
}

.mh-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--text3);
}

.mh-field input,
.mh-field select,
.history-panel button {
  font-family: var(--font-body);
  font-size: 13px;
}

.mh-field input,
.mh-field select {
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  min-height: 34px;
}
.mh-field input:focus,
.mh-field select:focus {
  border-color: var(--gold-dark);
}

.history-panel button {
  min-height: 34px;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
}
.history-panel button:hover:not(:disabled) {
  border-color: var(--gold-dark);
  color: var(--gold-light);
}
.history-panel button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
#mh-load {
  background: color-mix(in srgb, var(--gold) 18%, var(--surface));
  border-color: var(--gold-dark);
  color: var(--gold-light);
}
#mh-play[aria-pressed='true'] {
  border-color: var(--gold);
  color: var(--gold);
}

.mh-note {
  flex: 1 1 260px;
  margin: 0;
  font-size: 11.5px;
  line-height: 1.6;
  color: var(--text3);
  min-width: 220px;
}

.mh-playback {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px;
}
.mh-field-inline {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}
#mh-time {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--gold-light);
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface2);
}

.mh-scrub-label {
  font-size: 11px;
  color: var(--text3);
}
.mh-scrub {
  width: 100%;
  accent-color: var(--gold);
}
.mh-range-labels {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text3);
}

.mh-status {
  margin: 0;
  padding: 8px 10px;
  border-left: 3px solid var(--border-strong);
  background: var(--surface2);
  border-radius: 2px;
  font-size: 13px;
  line-height: 1.55;
  color: var(--text);
}
.mh-status[data-state='ready'] {
  border-left-color: #4caf50;
}
.mh-status[data-state='gap'] {
  border-left-color: #c97a2a;
}
.mh-status[data-state='error'] {
  border-left-color: #f44336;
}

.mh-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}
.mh-block {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--surface2) 65%, transparent);
}
.mh-block h3 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 13px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold-light);
}
.mh-block-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}
.mh-muted {
  margin: 0;
  font-size: 12px;
  line-height: 1.55;
  color: var(--text3);
}

.mh-gaps,
.mh-counts,
.mh-events {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.mh-gaps li,
.mh-counts li {
  font-size: 12px;
  line-height: 1.5;
  color: var(--text2);
}
.mh-gaps li {
  padding-left: 8px;
  border-left: 2px solid #c97a2a;
}
.mh-gaps li.mh-gap-marker {
  border-left-color: var(--border-strong);
  color: var(--text3);
  font-style: italic;
}
.mh-counts li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.mh-guild-name {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}
.mh-guild-swatch {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  flex: 0 0 auto;
}
.mh-count-value {
  font-family: var(--font-mono);
  color: var(--text);
  white-space: nowrap;
}

.mh-events-block {
  grid-column: span 2;
}
.mh-events li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 12.5px;
}
.mh-events li.is-current {
  border-color: var(--gold-dark);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--gold) 40%, transparent);
}
.mh-events time {
  font-family: var(--font-mono);
  color: var(--gold-light);
  white-space: nowrap;
}
.mh-event-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 8px;
  flex: 1 1 auto;
  min-width: 0;
}
.mh-event-summary span {
  color: var(--text3);
}
.mh-events li button {
  min-height: 28px;
  padding: 4px 10px;
  font-size: 12px;
}
.mh-list-empty {
  color: var(--text3);
  font-size: 12.5px;
  padding: 6px 2px;
}

/* History sidebar detail (replaces the live acquired/CD detail while the mode is active). */
.sb-header .mh-detail-title {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  font-weight: bold;
  font-family: 'Cinzel', serif;
  letter-spacing: 0.5px;
}
.sb-header button.sb-close {
  background: none;
  border: none;
  font-size: 16px;
  line-height: 1;
}
.sb-detail h3 {
  margin: 14px 16px 8px;
  font-family: var(--font-display);
}
.sb-detail p {
  margin: 6px 16px;
  font-size: 13.5px;
  line-height: 1.6;
}
.sb-detail p.mh-muted {
  color: var(--text3);
  font-size: 12.5px;
}

.history-toggle input:focus-visible,
.history-panel button:focus-visible,
.history-panel input:focus-visible,
.history-panel select:focus-visible,
.sb-header button:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

/* ── <768px ── */
@media (max-width: 767px) {
  .history-panel {
    max-height: 48vh;
    padding: 10px 12px 12px;
  }
  .map-stack #map {
    min-height: 200px;
  }
  .mh-grid {
    grid-template-columns: 1fr;
  }
  .mh-events-block {
    grid-column: span 1;
  }
  .mh-field input,
  .mh-field select {
    font-size: 16px; /* prevents iOS zoom on focus */
  }
  .page-inner:has(> .sidebar.open) .history-panel {
    padding-right: 12px; /* the sidebar is a bottom sheet here, not a side panel */
  }
}

/* ── <720px ── */
@media (max-width: 719px) {
  .map-header {
    flex-wrap: wrap;
    row-gap: 6px;
    padding: 8px 10px;
  }
  .map-search {
    order: 3;
    flex: 1 1 100%;
    max-width: none;
    margin: 0;
  }
  .history-toggle {
    margin-left: auto;
  }
  .mh-query,
  .mh-playback {
    gap: 8px;
  }
  .mh-field {
    flex: 1 1 140px;
  }
  .mh-field input,
  .mh-field select {
    width: 100%;
  }
  .mh-note {
    flex: 1 1 100%;
  }
  #mh-time {
    flex: 1 1 100%;
    text-align: center;
  }
  .mh-events li {
    flex-direction: column;
    align-items: flex-start;
  }
}

@media (prefers-reduced-motion: reduce) {
  .history-panel {
    transition: none !important;
  }
}

/* ── War advisor (/map) ──
   Read-only situation panel: a 400px left drawer on desktop and a 60vh bottom sheet below
   768px. Stacking: sticky navigation (3000) > war drawer (2600) > map content and the
   right territory sidebar. The trigger is a normal map-header control, not a sidebar overlay. */

.war-panel [hidden] {
  display: none !important;
}

.map-header {
  flex-wrap: wrap;
}

/* The territory sidebar overlays the map; keep header controls outside its footprint. */
@media (min-width: 769px) {
  .page-inner:has(> .sidebar.open) .map-header {
    right: var(--sidebar-w);
  }
}

/* Two desktop panels leave too little horizontal room for the trigger near the breakpoint.
   Keep the header above the left drawer instead of raising its stacking layer. */
@media (min-width: 769px) and (max-width: 879px) {
  .page-inner:has(> .sidebar.open) .map-header {
    min-height: 64px;
  }
  body:has(.sidebar.open) .war-panel {
    top: calc(var(--nav-height) + 64px);
  }
}

/* The existing territory sidebar becomes a 55% bottom sheet at exactly 768px.
   Keep the desktop left drawer above that sheet without changing the sidebar itself. */
@media (width: 768px) {
  body:has(.sidebar.open) .war-panel {
    bottom: calc((100vh - var(--nav-height)) * 0.55);
  }
}

.war-toggle {
  flex-shrink: 0;
  white-space: nowrap;
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--gold-light);
  font-family: var(--font-body);
  font-size: 13px;
  letter-spacing: 0.04em;
  cursor: pointer;
}
.war-toggle:hover {
  border-color: var(--gold);
  color: var(--text);
}

.war-panel {
  position: fixed;
  top: var(--nav-height);
  left: 0;
  bottom: 0;
  z-index: 2600;
  width: min(400px, 100vw);
  display: flex;
  flex-direction: column;
  background: color-mix(in srgb, var(--surface) 97%, transparent);
  border-right: 1px solid var(--border-strong);
  box-shadow: 18px 0 44px rgba(0, 0, 0, 0.3);
  transform: translateX(-100%);
  visibility: hidden;
  transition:
    transform 0.25s ease,
    visibility 0.25s ease;
}
.war-panel.is-open {
  transform: none;
  visibility: visible;
  /* Show immediately so the existing JS can focus the close button synchronously. */
  transition-duration: 0.25s, 0s;
}

.war-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
}
.war-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: 14px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold-light);
  white-space: nowrap;
}
.war-guild {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 32px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 13px;
}
.war-guild:disabled {
  opacity: 0.6;
}
.war-close {
  flex: 0 0 auto;
  min-width: 32px;
  min-height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text2);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}
.war-close:hover {
  border-color: var(--gold-dark);
  color: var(--text);
}

.war-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.war-sections,
.war-risk,
.war-risk-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.war-risk-list {
  gap: 8px;
}

.war-status {
  margin: 0;
  padding: 9px 10px;
  border-left: 3px solid var(--border-strong);
  border-radius: 2px;
  background: var(--surface2);
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
}
.war-status[data-state='loading'] {
  border-left-color: var(--gold-dark);
}
.war-status[data-state='empty'] {
  border-left-color: var(--orange);
}
.war-status[data-state='error'] {
  border-left-color: #f44336;
}

.war-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--surface2) 65%, transparent);
}
.war-section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px 10px;
}
.war-section-title,
.war-subtitle {
  margin: 0;
  font-family: var(--font-display);
  font-size: 12.5px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--gold-light);
}
.war-subtitle {
  font-size: 11.5px;
  letter-spacing: 0.08em;
  color: var(--text2);
}
/* One metric per row: the drawer is at most 400px wide on desktop, and a two-column grid
   breaks labels like 北京时间 mid-word in the bottom sheet. */
.war-metrics {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px 14px;
  margin: 0;
}
.war-metric {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
}
.war-metric-label {
  font-size: 11.5px;
  color: var(--text3);
  white-space: nowrap;
}
.war-metric-value {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text);
  text-align: right;
  overflow-wrap: break-word;
}

.war-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.war-list li {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 8px;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 12.5px;
}
.war-list strong {
  font-family: var(--font-mono);
  color: var(--gold-light);
}
.war-meta {
  font-size: 11.5px;
  color: var(--text3);
  overflow-wrap: anywhere;
}
.war-empty {
  margin: 0;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text3);
}

.war-card {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
}
.war-card[data-priority='high'] {
  border-left-color: #f44336;
}
.war-card[data-priority='medium'] {
  border-left-color: var(--orange);
}
.war-card-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 8px;
}
.war-card-title {
  margin: 0;
  flex: 1 1 auto;
  min-width: 0;
  font-size: 13px;
  color: var(--text);
}
.war-card-text {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--text2);
  overflow-wrap: anywhere;
}
.war-priority,
.war-tag {
  padding: 1px 6px;
  border: 1px solid var(--border-strong);
  border-radius: 2px;
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--text2);
  white-space: nowrap;
}
.war-priority[data-priority='high'] {
  border-color: #f44336;
  color: #f44336;
}
.war-priority[data-priority='medium'] {
  border-color: var(--orange);
  color: var(--orange);
}
.war-priority[data-priority='low'] {
  color: var(--text3);
}

.war-action {
  align-self: flex-start;
  min-height: 32px;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface2);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 13px;
  cursor: pointer;
}
.war-action:hover {
  border-color: var(--gold-dark);
  color: var(--gold-light);
}

.war-risk-name {
  font-family: var(--font-mono);
  color: var(--gold-light);
}
.war-risk-row[data-risk='high'] {
  border-left: 3px solid #f44336;
}
.war-risk-row[data-risk='medium'] {
  border-left: 3px solid var(--orange);
}
.war-risk-row[data-risk='low'] {
  border-left: 3px solid var(--green);
}

.war-source {
  flex: 0 0 auto;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--surface2);
}
.war-source-line {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.6;
  color: var(--text3);
  overflow-wrap: anywhere;
}
.war-source-line + .war-source-line {
  margin-top: 4px;
}

.war-toggle:focus-visible,
.war-panel button:focus-visible,
.war-panel select:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

/* ── <768px: bottom drawer, trigger stays in the map header ── */
@media (max-width: 767px) {
  .war-panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    height: 60vh;
    max-height: 60vh;
    border-right: none;
    border-top: 1px solid var(--border-strong);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -18px 42px rgba(0, 0, 0, 0.3);
    transform: translateY(100%);
  }
  .war-panel.is-open {
    transform: none;
  }
  .war-guild {
    font-size: 16px; /* prevents iOS zoom on focus */
  }
}

/* ── <720px: header controls wrap in flow and retain touch target sizes ── */
@media (max-width: 719px) {
  .war-section-head {
    gap: 4px;
  }
  .war-toggle,
  .war-action,
  .war-close {
    min-height: 40px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .war-panel,
  .war-toggle {
    transition: none !important;
  }
}

/* Inline search actions retain the territory label's typography and footprint. */
.war-territory-search,
.mh-events li .mh-territory-search {
  min-height: 0;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  text-align: inherit;
  cursor: pointer;
}
.war-territory-search:hover,
.war-territory-search:focus-visible,
.mh-territory-search:hover,
.mh-territory-search:focus-visible {
  text-decoration: underline;
  text-underline-offset: 2px;
}
