:root {
  color-scheme: dark;
  --background: #0a111e;
  --panel: #101c2c;
  --panel-soft: #111f31;
  --border: #25374b;
  --text: #e8f0f8;
  --muted: #7f96ac;
  --faint: #637c93;
  --accent: #50bfe6;
  --green: #55d98c;
  --red: #ff6b78;
  --yellow: #f6d77a;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: var(--background);
  color: var(--text);
  font-family: "Segoe UI", Arial, sans-serif;
  font-size: 14px;
}

button, input { font: inherit; }
button { color: inherit; }
button:focus-visible, input:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.app-header {
  height: 76px;
  padding: 0 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  background: #0e1828;
  border-bottom: 1px solid #223247;
}

.brand, .header-actions { display: flex; align-items: center; }
.brand { gap: 13px; }
.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid #2c637c;
  border-radius: 11px;
  background: #17364a;
  color: #68c9ed;
  font-size: 23px;
  font-weight: 700;
}
.brand-name { font-size: 20px; font-weight: 700; letter-spacing: .03em; }
.brand-subtitle { margin-top: 3px; color: var(--muted); font-size: 10px; letter-spacing: .08em; }
.header-actions { gap: 22px; }
.header-stats { display: flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.header-stats strong { font-size: 14px; }
.header-stats span { color: var(--muted); font-size: 11px; }

.button {
  min-height: 32px;
  padding: 6px 12px;
  border: 1px solid #2b4056;
  border-radius: 7px;
  background: #162537;
  color: #c9d8e8;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .15s;
}
.button:hover:not(:disabled) { background: #22384f; border-color: #4a6783; }
.button:active:not(:disabled) { transform: translateY(1px); }
.button:disabled { opacity: .55; cursor: wait; }
.button-primary { background: #176583; border-color: #2d91b5; color: #fff; font-weight: 600; }
.button-primary:hover:not(:disabled) { background: #1d789b; border-color: #55b9dc; }
.button-small { min-height: 27px; padding: 4px 8px; font-size: 11px; }
.icon-button { min-width: 34px; padding: 5px 9px; font-size: 18px; line-height: 1; }

.workspace {
  height: calc(100vh - 76px);
  min-height: 570px;
  padding: 22px 24px;
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 18px;
}
.sidebar { min-height: 0; display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 16px; }
.panel {
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 13px;
}
.alliances-panel { padding: 17px; }
.panel h2, .map-toolbar h2 {
  margin: 0;
  color: #9eb2c5;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .035em;
}
.muted { color: var(--muted); }
.panel-description { margin: 4px 0 13px; font-size: 12px; }
.alliance-list { display: grid; gap: 11px; }
.alliance-option {
  display: flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
}
.alliance-option input { width: 15px; height: 15px; margin: 0 3px 0 0; cursor: pointer; }
.alliance-color { width: 9px; height: 9px; flex: 0 0 9px; border-radius: 50%; }
.alliance-name { font-weight: 600; }
.alliance-count { margin-left: auto; color: #71899f; font-size: 11px; }
.filter-footer { margin-top: 15px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#filterHint { font-size: 10px; }
#filterHint.warning { color: var(--red); }

.nearest-panel { min-height: 0; padding: 17px; display: flex; flex-direction: column; }
.empty-state {
  max-width: 240px;
  margin: auto;
  padding: 25px 0;
  color: #627b92;
  font-size: 13px;
  line-height: 1.6;
  text-align: center;
}
.nearest-list {
  min-height: 0;
  margin: 0;
  padding: 0 3px 0 0;
  list-style: none;
  overflow-y: auto;
}
.nearest-list li { margin-bottom: 7px; }
.nearest-card {
  width: 100%;
  padding: 10px;
  display: grid;
  grid-template-columns: 25px minmax(0, 1fr) auto;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: var(--panel-soft);
  text-align: left;
  cursor: pointer;
}
.nearest-card:hover { background: #17283b; border-color: #2a4259; }
.nearest-rank {
  width: 23px;
  height: 23px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #1c3044;
  color: #8ea4b9;
  font-size: 10px;
}
.nearest-title { display: flex; align-items: center; gap: 7px; min-width: 0; }
.nearest-title i { width: 7px; height: 7px; flex: 0 0 7px; border-radius: 50%; }
.nearest-title strong { overflow: hidden; color: #b9ccdd; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.nearest-alliance { margin: 2px 0 0 14px; font-size: 10px; font-weight: 600; }
.nearest-details { margin: 4px 0 0 14px; color: #849aaf; font-size: 10px; }
.nearest-distance { align-self: center; text-align: right; }
.nearest-distance strong { display: block; font-size: 13px; }
.nearest-distance span { color: #71899f; font-size: 10px; }
.nearest-note { margin: 7px 0 0; color: #6f879d; font-size: 10px; }

.map-panel { min-height: 0; display: grid; grid-template-rows: 58px minmax(0, 1fr) 42px; overflow: hidden; }
.map-toolbar { padding: 0 17px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-bottom: 1px solid #223247; }
.map-toolbar p { margin: 3px 0 0; font-size: 11px; }
.map-controls { display: flex; gap: 5px; }
.map-viewport { position: relative; min-height: 0; overflow: hidden; background: #0c1625; }
#mapCanvas { width: 100%; height: 100%; display: block; touch-action: none; }
.grid-line { stroke: #1b2b3e; stroke-width: 1; }
.grid-label { fill: #435b72; font-size: 10px; }
.city-marker { cursor: pointer; }
.city-marker .visible-point { stroke: #e0eef8; stroke-width: 1; }
.city-marker.selected .visible-point { stroke-width: 2; }
.city-marker:hover .visible-point { stroke: #fff; stroke-width: 2; }
.city-label { fill: #b9ccdd; font-size: 11px; font-weight: 600; pointer-events: none; }
.city-label.selected { fill: var(--yellow); font-weight: 700; }
.map-empty { fill: #6f879d; font-size: 14px; }
.map-footer { padding: 0 17px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-top: 1px solid #223247; }
.legend { display: flex; gap: 17px; }
.legend > span { display: flex; align-items: center; gap: 6px; color: #8ea4b9; font-size: 11px; }
.ring-sample { width: 9px; height: 9px; border: 2px solid; border-radius: 50%; }
.ring-sample.comparable { border-color: var(--green); }
.ring-sample.different { border-color: var(--red); }
.map-hint { color: var(--faint); font-size: 10px; }

.city-popup {
  position: absolute;
  z-index: 10;
  width: 260px;
  padding: 16px;
  border: 1px solid #49647e;
  border-radius: 11px;
  background: #17283b;
  box-shadow: 0 14px 35px #0007;
}
.city-popup h3 { margin: 0; padding-right: 20px; font-size: 18px; }
.popup-close { position: absolute; top: 7px; right: 9px; border: 0; background: transparent; color: #8ea4b9; font-size: 22px; cursor: pointer; }
.popup-alliance { margin: 2px 0 12px; font-size: 12px; font-weight: 600; }
.city-popup dl { margin: 0; }
.city-popup dl div { margin-top: 7px; display: flex; justify-content: space-between; gap: 10px; }
.city-popup dt { color: #8ea4b9; font-size: 12px; }
.city-popup dd { margin: 0; font-size: 12px; font-weight: 600; }
.popup-note { margin: 12px 0 0; padding-top: 9px; border-top: 1px solid #30465c; color: #8ea4b9; font-size: 11px; line-height: 1.45; }

.settings-dialog {
  width: min(1080px, calc(100vw - 40px));
  max-height: 90vh;
  padding: 0;
  border: 1px solid #38516a;
  border-radius: 15px;
  background: #0f1b2b;
  color: var(--text);
  box-shadow: 0 25px 90px #0009;
}
.settings-dialog::backdrop { background: #030913c9; backdrop-filter: blur(3px); }
.dialog-header { padding: 20px 22px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border); }
.dialog-header h2 { margin: 0; font-size: 20px; }
.dialog-header p { margin: 4px 0 0; font-size: 12px; }
.settings-view { padding: 20px 22px 22px; }
.settings-toolbar { margin-bottom: 16px; display: flex; align-items: center; justify-content: space-between; gap: 15px; }
.settings-toolbar p { margin: 0; font-size: 12px; }
code { color: #a8c4d9; font-family: Consolas, monospace; }
.city-table-container { max-height: 58vh; overflow: auto; border: 1px solid var(--border); border-radius: 10px; }
.city-table { width: 100%; border-collapse: collapse; min-width: 790px; }
.city-table th { position: sticky; top: 0; z-index: 1; padding: 11px 12px; background: #162639; color: #8ea4b9; font-size: 10px; letter-spacing: .04em; text-align: left; text-transform: uppercase; }
.city-table td { padding: 11px 12px; border-top: 1px solid #203247; color: #b9ccdd; font-size: 12px; }
.city-table tbody tr:hover { background: #142438; }
.city-table td.numeric { font-variant-numeric: tabular-nums; }
.city-alliance { font-weight: 600; }
.city-actions { display: flex; justify-content: flex-end; gap: 6px; }
.city-actions .button { min-height: 28px; padding: 4px 9px; font-size: 11px; }
.danger-button:hover:not(:disabled) { border-color: #a94c58; background: #512934; color: #ffb3ba; }
.empty-table { padding: 35px !important; color: var(--muted) !important; text-align: center; }
.editor-view { max-width: 720px; margin: 0 auto; }
.back-button { padding: 0; border: 0; background: transparent; color: var(--accent); cursor: pointer; }
.editor-view h3 { margin: 15px 0 20px; font-size: 20px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 17px; }
.form-grid label { display: grid; gap: 7px; color: #9eb2c5; font-size: 12px; }
.form-grid input {
  width: 100%;
  padding: 10px 11px;
  border: 1px solid #304861;
  border-radius: 7px;
  background: #0b1624;
  color: var(--text);
}
.form-grid input:invalid:not(:placeholder-shown) { border-color: #a94c58; }
.form-error { min-height: 20px; margin: 13px 0 0; color: #ff96a0; font-size: 12px; }
.form-actions { margin-top: 10px; display: flex; justify-content: flex-end; gap: 8px; }
.toast { position: fixed; right: 25px; bottom: 25px; z-index: 100; max-width: 380px; padding: 13px 17px; border: 1px solid #3d7960; border-radius: 9px; background: #163b30; color: #c9f4df; box-shadow: 0 10px 30px #0006; }
.toast.error { border-color: #9d4652; background: #46242c; color: #ffc2c8; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

@media (max-width: 950px) {
  .workspace { height: auto; grid-template-columns: 1fr; }
  .sidebar { grid-template-columns: 1fr 1fr; grid-template-rows: auto; }
  .map-panel { min-height: 620px; }
  .header-stats { display: none; }
  .nearest-panel { min-height: 330px; }
}

@media (max-width: 650px) {
  .app-header { padding: 0 15px; }
  .brand-subtitle { display: none; }
  .brand-name { font-size: 17px; }
  .header-actions { gap: 8px; }
  .workspace { padding: 14px; }
  .sidebar { grid-template-columns: 1fr; }
  .map-toolbar { padding: 10px 12px; align-items: flex-start; }
  .map-toolbar h2 { margin-top: 5px; }
  .map-footer { align-items: flex-start; padding: 10px 13px; }
  .map-hint { display: none; }
  .map-panel { grid-template-rows: 72px minmax(0, 1fr) 50px; }
  .form-grid { grid-template-columns: 1fr; }
  .settings-toolbar { align-items: flex-start; }
  .settings-toolbar p { max-width: 200px; }
}
