/* Page shell, topbar, sidebar, map stage controls, dialogs and responsive framing. */
.topbar {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-paper);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  text-decoration: none;
}

.brand-symbol {
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  font-size: 24px;
  line-height: 1;
  color: var(--accent);
}

.brand strong {
  font: 600 20px/1.2 var(--font-serif);
  letter-spacing: 0.08em;
}

.era-picker {
  display: flex;
  align-self: stretch;
  gap: 8px;
}

.era-picker button {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  padding: 0 12px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--ink-muted);
  align-self: stretch;
  align-items: center;
}

.era-picker button:hover {
  color: var(--ink);
}

.era-picker b {
  font: 600 var(--text-heading)/1.2 var(--font-sans);
  font-variant-numeric: tabular-nums;
}

.era-picker span {
  font-size: var(--text-caption);
  color: var(--ink-muted);
}

.era-picker .active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.topbar > .era-picker.atlas-segmented .active {
  border-bottom-color: transparent;
}

.topbar > .era-picker.atlas-segmented::before {
  top: auto;
  bottom: -1px;
  height: 2px !important;
  border-radius: 1px;
  background: var(--accent);
  box-shadow: none;
}

.era-picker .active span {
  color: var(--accent);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-actions .plain {
  min-height: 36px;
  padding: 4px 0;
  font-size: var(--text-caption);
  white-space: nowrap;
}

.sound-toggle.plain {
  gap: 6px;
}

.sound-toggle[aria-pressed="false"] .icon-sound-on,
.sound-toggle[aria-pressed="true"] .icon-sound-off {
  display: none;
}

.topbar .sound-toggle.plain {
  width: 36px;
  height: 36px;
  padding: 0;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--ink-muted);
  text-decoration: none;
}

.topbar .sound-toggle.plain:hover {
  background: var(--bg-subtle);
  border-color: var(--line);
  color: var(--accent);
  text-decoration: none;
}

.topbar .sound-toggle[aria-pressed="true"] {
  color: var(--accent);
}

.topbar .sound-label,
.topbar .sound-state {
  display: none;
}

.sound-state {
  margin-left: auto;
  padding: 2px 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius-xs);
  color: var(--accent);
  background: var(--accent-soft);
  font-size: var(--text-caption);
}

.sound-toggle[aria-pressed="false"] .sound-state {
  color: var(--ink-muted);
  background: transparent;
}

.workspace {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  height: calc(100dvh - 60px);
  min-height: 400px;
}

.sidebar {
  padding: 16px 20px;
  border-right: 1px solid var(--line);
  background: var(--bg-paper);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.mobile-context {
  display: none;
}

.mode-tabs {
  display: flex;
  background: var(--bg-subtle);
  border-radius: var(--radius-md);
  padding: 4px;
  flex-shrink: 0;
}

.mode-tabs button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 50%;
  min-height: 36px;
  padding: 4px 8px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink-muted);
  font-size: var(--text-body);
  font-weight: 500;
}

.mode-tabs .active {
  background: var(--bg-surface);
  color: var(--accent);
  font-weight: 600;
}

.map-stage {
  position: relative;
  min-width: 0;
  overflow: hidden;
  background: #587c9c;
  user-select: none;
  -webkit-user-select: none;
}

#map {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}

#map.dragging {
  cursor: grabbing;
}

#map:focus-visible {
  outline-offset: -4px;
}

.map-toolbar {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 6;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  max-width: calc(100% - 32px);
  pointer-events: none;
}

.map-toolbar > .map-mode-control,
.map-toolbar > .display-panel {
  position: relative;
  pointer-events: auto;
  font-size: var(--text-caption);
  color: var(--ink);
}

.map-toolbar summary,
.map-legend > summary {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  color: var(--ink);
  font-size: var(--text-caption);
  list-style: none;
  white-space: nowrap;
  box-shadow: var(--shadow-float);
}

.map-toolbar summary::-webkit-details-marker,
.map-legend > summary::-webkit-details-marker {
  display: none;
}

.map-toolbar summary::after {
  content: '▾';
  color: var(--ink-muted);
}

.map-toolbar details[open] > summary::after {
  content: '▴';
}

.map-toolbar summary strong,
.map-legend > summary strong {
  color: var(--accent);
  font-weight: 600;
}

.layer-controls {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  width: 208px;
  padding: 12px;
  background: var(--bg-surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-float);
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: var(--text-body);
}

.layer-controls label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.layer-controls .subject-control {
  border-top: 1px solid var(--line);
  padding-top: 12px;
  font-size: var(--text-caption);
  color: var(--ink-muted);
}

.layer-controls select {
  margin-top: -4px;
}

.map-controls {
  position: absolute;
  right: 16px;
  bottom: 16px;
  z-index: 4;
  display: flex;
  flex-direction: column;
  background: var(--bg-surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-float);
  overflow: hidden;
}

.map-controls button {
  width: 36px;
  height: 36px;
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: 20px;
  line-height: 1;
  display: grid;
  place-items: center;
}

.map-controls button:hover {
  background: var(--bg-subtle);
}

.map-controls button + button {
  border-top: 1px solid var(--line);
}

.map-tooltip {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  box-shadow: var(--shadow-float);
  max-width: 256px;
}

.map-tooltip.with-flag {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  max-width: 284px;
}

.map-tooltip .tooltip-flag {
  width: 36px;
  height: 24px;
}

.map-tooltip .tooltip-body {
  min-width: 0;
}

.map-tooltip strong {
  display: block;
  font-size: var(--text-body);
  font-weight: 600;
  line-height: 1.35;
}

.map-tooltip span {
  display: block;
  margin-top: 2px;
  font-size: var(--text-caption);
  color: var(--ink-muted);
  line-height: 1.35;
}

.modern-dispute-legend {
  position: absolute;
  z-index: 3;
  left: 16px;
  bottom: 16px;
  margin: 0;
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  color: var(--ink);
  font-size: var(--text-caption);
  pointer-events: none;
}

.answer-locator {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  transform: translate(-50%, -50%);
  display: grid;
  place-items: center;
}

.answer-locator-ring {
  display: block;
  width: 28px;
  height: 28px;
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 2px var(--accent), var(--shadow-float);
}

.answer-locator-label {
  position: absolute;
  bottom: calc(100% + 4px);
  padding: 2px 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius-xs);
  background: var(--bg-surface);
  color: var(--accent);
  font-size: var(--text-caption);
  font-weight: 600;
  white-space: nowrap;
}

.map-wrong-name {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  padding: 4px 8px;
  font-size: var(--text-caption);
  line-height: 1.4;
  border-radius: var(--radius-xs);
  border: 1px solid var(--line);
  background: var(--bg-surface);
  color: var(--ink);
  max-width: 88%;
}

.map-error {
  position: absolute;
  top: 16px;
  left: 16px;
  right: 16px;
  z-index: 7;
  padding: 12px;
  background: var(--danger-soft);
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  color: var(--danger);
  font-size: var(--text-body);
}

dialog {
  margin: auto;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  width: calc(100% - 32px);
  max-width: 560px;
  max-height: min(680px, calc(100dvh - 80px));
  background: var(--bg-paper);
  color: var(--ink);
  box-shadow: 0 16px 48px rgba(28, 36, 30, 0.24);
  overflow: hidden;
}

dialog[open] {
  display: flex;
  flex-direction: column;
}

dialog::backdrop {
  background: rgba(28, 36, 30, 0.42);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.dialog-header h2 {
  margin: 0;
  font: 600 var(--text-title)/1.25 var(--font-serif);
}

.dialog-close {
  min-width: 36px;
  min-height: 36px;
  justify-content: center;
  font-size: 20px;
  padding: 0 8px;
}

.dialog-body {
  padding: 16px 20px;
  overflow-y: auto;
  overscroll-behavior: contain;
  flex: 1;
  min-height: 0;
}

.dialog-body h3 {
  margin: 16px 0 8px;
  font-size: var(--text-heading);
  font-weight: 600;
}

.dialog-body h3:first-child {
  margin-top: 0;
}

.dialog-body p {
  margin: 0 0 12px;
  color: var(--ink-muted);
  font-size: var(--text-body);
  line-height: 1.6;
}

.dialog-body small {
  display: block;
  margin-top: 12px;
  color: var(--ink-muted);
  font-size: var(--text-caption);
}

.dialog-body a {
  color: var(--accent);
}

.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  padding: 12px 20px;
  border-top: 1px solid var(--line);
  background: var(--bg-paper);
  flex-shrink: 0;
}

.source-box {
  background: var(--bg-subtle);
  border-radius: var(--radius-md);
  padding: 12px;
  margin-bottom: 12px;
  font-size: var(--text-caption);
  line-height: 1.65;
  color: var(--ink);
}

@media (min-width: 1600px) {
  .workspace {
    grid-template-columns: 360px minmax(0, 1fr);
  }
}

@media (max-width: 1000px) and (min-width: 761px) {
  .workspace {
    grid-template-columns: 312px minmax(0, 1fr);
  }
  .sidebar {
    padding: 16px;
  }
  .topbar {
    padding: 0 16px;
  }
}

/* Mobile 1-screen viewport layout */
body.mobile-layout {
  height: 100dvh;
  overflow: hidden;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

body.mobile-layout > .topbar,
body.mobile-layout .mode-tabs {
  display: none;
}

body.mobile-layout .workspace {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

body.mobile-layout .sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 8px 12px;
  gap: 8px;
  border-right: 0;
  border-bottom: 1px solid var(--line);
  overflow: visible;
}

body.mobile-layout .mobile-context {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  font-size: var(--text-caption);
}

body.mobile-layout[data-mode="learn"] .mobile-context,
body.mobile-layout.round-complete .mobile-context {
  grid-template-columns: minmax(0, 1fr) auto;
}

body.mobile-layout[data-mode="learn"] #mobile-practice-actions,
body.mobile-layout.round-complete #mobile-practice-actions {
  display: none;
}

.mobile-context-main {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

#mobile-mode {
  width: 96px;
  height: 44px;
  min-height: 44px;
  padding: 0 24px 0 8px;
  font-size: var(--text-caption);
  font-weight: 600;
  color: var(--accent);
  background-color: var(--bg-surface);
  flex-shrink: 0;
}

#mobile-context-text {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--text-caption);
  color: var(--ink-muted);
}

body.mobile-layout[data-mode="quiz"]:not(.round-complete) #mobile-context-text,
body[data-mode="learn"] #mobile-round-progress,
body.round-complete #mobile-round-progress {
  display: none;
}

#mobile-round-progress,
body.mobile-layout #mobile-settings-open,
body.mobile-layout #mobile-practice-actions #restart {
  min-width: 44px;
  min-height: 44px;
  padding: 0 8px;
  justify-content: center;
  font-size: var(--text-caption);
  white-space: nowrap;
}

#mobile-round-progress {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

#mobile-round-progress::after {
  content: '⌄';
  margin-left: 4px;
}

#mobile-round-progress[aria-expanded="true"]::after {
  content: '⌃';
}

body.mobile-layout #mobile-settings-open {
  max-width: 108px;
  overflow: hidden;
  text-overflow: ellipsis;
}

body.mobile-layout .map-stage {
  height: auto;
  min-height: 0;
  overflow: hidden;
}

body.mobile-layout .map-toolbar {
  top: 12px;
  left: 12px;
  max-width: calc(100% - 24px);
}

body.mobile-layout .map-toolbar summary,
body.mobile-layout .map-legend > summary {
  min-height: 44px;
  padding: 0 10px;
}

body.mobile-layout .map-toolbar .layer-controls {
  left: auto;
  right: 0;
  max-height: 55dvh;
  overflow: auto;
}

body.mobile-layout .map-toolbar > .display-panel:first-child .layer-controls,
body.mobile-layout .map-mode-control.hidden + .display-panel .layer-controls {
  left: 0;
  right: auto;
}

body.mobile-layout .map-controls {
  right: 12px;
  bottom: 12px;
}

body.mobile-layout .map-controls button {
  width: 44px;
  height: 44px;
}

body.mobile-layout .modern-dispute-legend {
  left: 12px;
  bottom: 12px;
}

.mobile-settings-dialog {
  width: calc(100% - 24px);
  max-width: 480px;
  max-height: calc(100dvh - 32px);
}

.mobile-settings-dialog header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.mobile-settings-dialog header h2 {
  margin: 0;
  font: 600 var(--text-title)/1.25 var(--font-serif);
}

#mobile-settings-close {
  min-height: 44px;
  padding: 0 8px;
}

.mobile-settings-body {
  padding: 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1;
  min-height: 0;
}

#mobile-settings-content,
#mobile-explore-settings {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#mobile-settings-content > .era-picker {
  min-height: 44px;
  justify-content: space-between;
  background: var(--bg-subtle);
  border-radius: var(--radius-md);
  padding: 4px;
}

#mobile-settings-content > .era-picker button {
  flex: 1;
  justify-content: center;
  border-bottom: 0;
  border-radius: var(--radius-sm);
  min-height: 36px;
}

#mobile-settings-content > .era-picker button.active {
  background: var(--bg-surface);
}

#mobile-settings-content > .era-picker.atlas-segmented::before {
  top: 0;
  bottom: auto;
  height: var(--segment-height, 0) !important;
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  box-shadow: 0 1px 3px rgba(28, 36, 30, 0.08);
}

#mobile-settings-content > .sound-toggle,
#mobile-settings-content > #changelog-open,
#mobile-settings-content > #about {
  min-height: 44px;
  justify-content: space-between;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  color: var(--ink);
  text-decoration: none;
}

@media (max-width: 340px) {
  #mobile-mode {
    width: 88px;
  }
  .mobile-context-main,
  body.mobile-layout .mobile-context {
    gap: 4px;
  }
  body.mobile-layout #mobile-practice-actions #restart {
    padding: 0 4px;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  body.mobile-layout .workspace {
    grid-template-columns: minmax(260px, 36%) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
  }
  body.mobile-layout .sidebar {
    overflow-y: auto;
    border-bottom: 0;
    border-right: 1px solid var(--line);
    padding: 8px 12px;
  }
}
