:root {
  color-scheme: light;
  --bg: #eef2f0;
  --panel: #fffefa;
  --ink: #1e2623;
  --muted: #657069;
  --line: #d7ded8;
  --accent: #16675b;
  --accent-strong: #0d4d44;
  --gold: #a66d1d;
  --done: #1f7a4f;
  --danger: #9d332c;
  --shadow: 0 18px 44px rgba(28, 38, 34, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Microsoft YaHei", "PingFang SC", "Noto Sans CJK SC", system-ui, sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 38px;
  border: 0;
  border-radius: 6px;
  padding: 0 14px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
}

button:hover {
  background: var(--accent-strong);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

button.secondary {
  border: 1px solid var(--line);
  background: #f8faf7;
  color: var(--ink);
}

button.secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
}

input,
select,
textarea {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  padding: 0 10px;
}

textarea {
  min-height: 150px;
  padding: 10px;
  resize: vertical;
  line-height: 1.5;
}

.app {
  width: min(1480px, 100%);
  margin: 0 auto;
  padding: 24px clamp(14px, 3vw, 34px) 34px;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 18px;
  align-items: end;
  margin-bottom: 18px;
}

.topbar-tools {
  display: grid;
  justify-items: end;
  gap: 10px;
}

.back-link {
  display: inline-flex;
  margin-bottom: 0;
  color: var(--accent);
  font-weight: 800;
  text-decoration: none;
}

h1,
p {
  margin: 0;
}

h1 {
  font-size: clamp(28px, 4vw, 44px);
  line-height: 1.08;
}

.cycle-title {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.cycle-title .cycle-symbol {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  object-fit: contain;
}

.topbar p {
  margin-top: 8px;
  color: var(--muted);
}

.cycle-tabs {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.cycle-tabs button {
  min-width: 92px;
  background: #5e574b;
}

.cycle-tabs button.active {
  background: var(--accent);
}

.control-panel {
  display: grid;
  grid-template-columns: minmax(130px, 160px) minmax(240px, 270px) minmax(160px, 180px) minmax(0, 1fr);
  gap: 12px;
  align-items: end;
  margin-bottom: 14px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.sticky-actions {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  gap: 10px;
  margin-bottom: 14px;
  padding-top: 8px;
  background: var(--bg);
}

.sticky-actions .token-panel {
  margin-bottom: 0;
}

.sticky-actions .control-panel {
  margin-bottom: 0;
}

.token-panel {
  display: grid;
  grid-template-columns: 190px minmax(360px, 1fr) auto;
  grid-template-areas: "status tokens actions";
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.map-status {
  grid-area: status;
  min-width: 0;
  display: grid;
  gap: 2px;
}

.map-status strong {
  color: var(--gold);
  font-size: 16px;
  white-space: nowrap;
}

.map-status span {
  color: var(--muted);
  display: block;
  overflow: hidden;
  font-size: 12px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.second-screen-mode-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  font-weight: 700;
  cursor: pointer;
}

.second-screen-mode-toggle input {
  width: 16px;
  height: 16px;
  min-height: 0;
  margin: 0;
  accent-color: var(--accent);
}

.second-screen-mode-toggle.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.campaign-save-status[data-state="saving"] {
  color: var(--accent);
}

.campaign-save-status[data-state="offline"] {
  color: var(--danger);
  font-weight: 700;
}

.campaign-save-status[data-state="saved"] {
  color: #16794a;
}

.tool-cluster {
  grid-area: tokens;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.toolbar-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0;
  white-space: nowrap;
}

.token-palette {
  display: flex;
  flex-wrap: nowrap;
  gap: 5px;
  min-width: 0;
  overflow: visible;
}

.token-cluster {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-areas:
    "token-label token-palette"
    ". edge-directions";
  align-content: center;
  row-gap: 4px;
}

.token-cluster > .toolbar-label {
  grid-area: token-label;
}

.token-cluster > .token-palette {
  grid-area: token-palette;
}

.edge-direction-control {
  grid-area: edge-directions;
  justify-self: start;
  display: inline-grid;
  grid-template-columns: repeat(4, 30px);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: #fff;
}

.edge-direction-control[hidden] {
  display: none;
}

.edge-direction-control button {
  width: 30px;
  min-height: 32px;
  padding: 0;
  border: 0;
  border-right: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: var(--ink);
  font-size: 18px;
  line-height: 1;
}

.edge-direction-control button:last-child {
  border-right: 0;
}

.edge-direction-control button.active {
  background: var(--accent);
  color: #fff;
}

.map-actions {
  grid-area: actions;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 5px;
  margin-left: auto;
}

.action-group {
  display: inline-flex;
  align-items: center;
  flex-wrap: nowrap;
  gap: 4px;
  min-height: 34px;
  padding: 2px 4px 2px 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8faf7;
}

.titan-x-track[hidden] {
  display: none;
}

.titan-x-track-spaces {
  display: inline-flex;
  align-items: center;
  gap: 2px;
}

.map-actions .titan-x-track-space {
  min-width: 25px;
  min-height: 28px;
  padding: 0 4px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #fff;
  color: var(--ink);
}

.map-actions .titan-x-track-space.trigger {
  border-color: var(--danger);
  color: var(--danger);
}

.map-actions .titan-x-track-space.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.titan-x-track-status {
  min-width: 42px;
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
}

.map-actions button {
  min-height: 30px;
  padding: 0 8px;
  font-size: 12px;
}

.scout-count {
  min-height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 30px;
  padding: 0 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--accent-strong);
  font-weight: 900;
  white-space: nowrap;
}

.token-tool {
  min-width: 34px;
  min-height: 34px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px;
  background: #fff;
  color: var(--ink);
}

.token-tool.active {
  border-color: var(--gold);
  box-shadow: 0 0 0 2px rgba(166, 109, 29, 0.22);
}

.token-tool img {
  width: 26px;
  height: 26px;
  object-fit: contain;
  display: block;
}

.token-tool span {
  font-size: 12px;
  font-weight: 900;
}

.token-action-tool {
  grid-column: span 2;
  padding-inline: 6px;
}

.token-menu {
  position: relative;
  display: inline-flex;
}

.token-menu > summary {
  list-style: none;
}

.token-menu > summary::-webkit-details-marker {
  display: none;
}

.token-menu-trigger span::after {
  content: "";
  width: 0;
  height: 0;
  margin-left: 4px;
  border-inline: 4px solid transparent;
  border-top: 5px solid currentColor;
}

.token-menu-trigger span {
  display: inline-flex;
  align-items: center;
}

.token-submenu {
  position: absolute;
  z-index: 20;
  top: calc(100% + 6px);
  left: 0;
  display: grid;
  grid-template-columns: repeat(4, 34px);
  gap: 5px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.control-panel label {
  min-width: 0;
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

#searchInput {
  max-width: 150px;
}

.tile-lookup {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.tile-lookup-row {
  display: grid;
  grid-template-columns: minmax(0, 150px) max-content;
  justify-content: start;
  align-items: center;
  gap: 6px;
}

.tile-lookup-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.tile-lookup-actions button {
  min-height: 40px;
  padding: 0 8px;
  font-size: 12px;
}

.toggle-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.check-label {
  min-height: 40px;
  display: inline-flex !important;
  grid-template-columns: none !important;
  align-items: center;
  gap: 7px !important;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  white-space: nowrap;
}

.check-label input {
  width: 18px;
  min-height: 18px;
  accent-color: var(--done);
}

.zoom-control {
  min-width: 0;
  max-width: 180px;
}

.zoom-row {
  display: grid;
  grid-template-columns: minmax(80px, 1fr) 46px;
  align-items: center;
  gap: 10px;
}

.zoom-row input {
  width: 100%;
  accent-color: var(--done);
}

.zoom-row output {
  min-height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 900;
}

.tile-grid-viewport {
  width: 100%;
  max-height: calc(100vh - 210px);
  min-height: 280px;
  overflow: auto;
  border-radius: 8px;
  background: #151816;
  overscroll-behavior: contain;
}

.tile-grid {
  position: relative;
  width: var(--map-zoom, 100%);
  min-width: 320px;
  aspect-ratio: var(--map-ratio, 1 / 1);
  background: #151816;
}

.tile-card {
  position: absolute;
  overflow: hidden;
  border: 0;
  border-radius: 0;
  background: #151816;
  box-shadow: none;
  cursor: pointer;
}

.tile-card.current {
  z-index: 1;
}

.tile-card.ad-spawn-candidate {
  z-index: 5;
  filter: saturate(1.18) brightness(1.12);
  outline: 3px solid #f0b832;
  outline-offset: -3px;
}

.tile-card.ad-spawn-candidate::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 4;
  border: 3px solid rgba(240, 184, 50, 0.86);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.72), 0 0 18px rgba(240, 184, 50, 0.72);
  pointer-events: none;
}

.tile-card.explored {
  filter: saturate(1.08) brightness(1.04);
}

.tile-card.explored.ad-spawn-candidate {
  filter: saturate(1.22) brightness(1.14);
}

.tile-card.explored::before {
  content: "";
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 2;
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--done);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.72);
  pointer-events: none;
}

.tile-card.has-note.explored::before {
  right: 24px;
}

.tile-note-marker {
  position: absolute;
  top: 5px;
  right: 5px;
  z-index: 5;
  width: 14px;
  height: 14px;
  border-radius: 4px 4px 4px 1px;
  background: #f0b832;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.82), 0 2px 8px rgba(0, 0, 0, 0.28);
  pointer-events: none;
}

.tile-note-marker::after {
  content: "";
  position: absolute;
  right: 0;
  bottom: 0;
  width: 6px;
  height: 6px;
  border-top: 1px solid rgba(0, 0, 0, 0.22);
  border-left: 1px solid rgba(0, 0, 0, 0.16);
  background: rgba(255, 255, 255, 0.45);
}

.tile-tokens {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  gap: 3px;
  padding: 5px;
  pointer-events: none;
}

.map-token {
  width: 22%;
  max-width: 34px;
  min-width: 18px;
  aspect-ratio: 1 / 1;
  border-radius: 999px;
  background: transparent;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
  object-fit: contain;
}

.map-token.token-ag {
  width: 30%;
  max-width: 44px;
  min-width: 24px;
}

.map-token.token-ad {
  background: transparent;
}

.map-token.token-square {
  border-radius: 4px;
}

.tile-edge-tokens {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.map-edge-token {
  position: absolute;
  width: 24%;
  max-width: 38px;
  min-width: 20px;
  aspect-ratio: 1 / 1;
  border-radius: 4px;
  object-fit: contain;
  background: transparent;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.48);
}

.map-edge-token.edge-up {
  top: 3px;
  left: 50%;
  transform: translateX(-50%);
}

.map-edge-token.edge-right {
  top: 50%;
  right: 3px;
  transform: translateY(-50%);
}

.map-edge-token.edge-down {
  bottom: 3px;
  left: 50%;
  transform: translateX(-50%);
}

.map-edge-token.edge-left {
  top: 50%;
  left: 3px;
  transform: translateY(-50%);
}

.tile-image {
  width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  background: #141714;
}

.tile-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

body.second-screen-map {
  overflow: hidden;
  background: #151816;
}

body.second-screen-map .app {
  width: 100%;
  height: 100vh;
  padding: 0;
}

body.second-screen-map .topbar,
body.second-screen-map .sticky-actions,
body.second-screen-map .note-dialog {
  display: none !important;
}

body.second-screen-map .tile-grid-viewport {
  width: 100%;
  height: 100vh;
  max-height: none;
  border-radius: 0;
  touch-action: pan-x pan-y;
}

body.second-screen-map .tile-grid {
  margin: 50vh 50vw;
  pointer-events: none;
}

.tile-blue-cover {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background:
    radial-gradient(circle at 50% 38%, rgba(25, 91, 121, 0.34), transparent 48%),
    #04253a;
  color: #d8f3ff;
  box-shadow: inset 0 0 0 2px rgba(122, 194, 219, 0.2);
  font-size: clamp(16px, 2.4vw, 34px);
  font-weight: 900;
  letter-spacing: 0.08em;
  line-height: 1;
  text-shadow: 0 2px 8px rgba(0, 7, 14, 0.72);
}

.tile-variant-toggle {
  position: absolute;
  top: 5px;
  right: 22px;
  bottom: auto;
  z-index: 8;
  width: 16px;
  min-height: 16px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 3px;
  background: transparent;
  color: rgba(255, 255, 255, 0.86);
  font-size: 8px;
  font-weight: 900;
  line-height: 1;
}

.tile-variant-toggle:hover {
  border-color: rgba(240, 184, 50, 0.58);
  background: rgba(155, 103, 10, 0.28);
  color: rgba(255, 255, 255, 0.92);
}

.tile-variant-toggle.active {
  border-color: transparent;
  background: transparent;
  color: rgba(240, 184, 50, 0.94);
}

.tile-card.has-note.explored .tile-variant-toggle {
  right: 40px;
}

.tile-variant-toggle.c5-face-toggle,
.tile-variant-toggle.c5-face-toggle:hover,
.tile-variant-toggle.c5-face-toggle.active {
  top: 5px;
  bottom: auto;
  overflow: hidden;
  border-color: transparent;
  background: transparent;
}

.c5-face-toggle img {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 2px;
  object-fit: contain;
  opacity: 0.84;
}

.c5-face-toggle:hover img {
  opacity: 1;
}

.adjacency-labels {
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
  font-size: clamp(7px, 0.72vw, 11px);
  font-weight: 900;
  line-height: 1;
}

.adjacency-badge {
  position: absolute;
  max-width: 92%;
  padding: 2px 4px;
  border: 1px solid rgba(255, 255, 255, 0.68);
  border-radius: 4px;
  background: rgba(9, 14, 13, 0.78);
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
  white-space: nowrap;
}

.adjacency-badge.mismatch {
  border-color: rgba(255, 208, 82, 0.95);
  background: rgba(120, 67, 0, 0.86);
}

.adjacency-top {
  top: 3px;
  left: 50%;
  transform: translateX(-50%);
}

.adjacency-right {
  top: 50%;
  right: 3px;
  transform: translateY(-50%);
}

.adjacency-bottom {
  bottom: 3px;
  left: 50%;
  transform: translateX(-50%);
}

.adjacency-left {
  top: 50%;
  left: 3px;
  transform: translateY(-50%);
}

.adjacency-special {
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
}

.adjacency-special + .adjacency-special {
  bottom: 39px;
}

.adjacency-special + .adjacency-special + .adjacency-special {
  bottom: 56px;
}

.empty {
  position: static;
  padding: 28px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  text-align: center;
}

.note-dialog {
  width: min(560px, calc(100vw - 28px));
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
}

.note-dialog::backdrop {
  background: rgba(10, 18, 15, 0.42);
}

.note-card {
  display: grid;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.note-card-header,
.note-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.note-card-header strong {
  color: var(--accent-strong);
  font-size: 18px;
}

.icon-button {
  width: 36px;
  min-height: 36px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #f8faf7;
  color: var(--ink);
  font-size: 24px;
  line-height: 1;
}

button.danger {
  border-color: rgba(157, 51, 44, 0.28);
  color: var(--danger);
}

@media (max-width: 900px) {
  .app {
    padding: 18px 16px 26px;
  }

  .topbar {
    grid-template-columns: 1fr auto;
    align-items: end;
    gap: 10px;
    margin-bottom: 12px;
  }

  h1 {
    font-size: 34px;
  }

  .topbar p {
    margin-top: 4px;
    font-size: 14px;
  }

  .back-link {
    margin-bottom: 6px;
  }

  .cycle-tabs {
    justify-content: flex-end;
    gap: 6px;
  }

  .cycle-tabs button {
    min-width: 82px;
    min-height: 34px;
    padding: 0 10px;
  }

  .control-panel {
    grid-template-columns: minmax(130px, 160px) minmax(240px, 270px) minmax(160px, 180px);
    gap: 8px;
    margin-bottom: 8px;
    padding: 10px;
  }

  .toggle-row {
    grid-column: 1 / -1;
  }

  input,
  select {
    min-height: 36px;
  }

  .check-label {
    min-height: 36px;
    padding: 0 8px;
    font-size: 12px;
  }

  .sticky-actions {
    gap: 8px;
    margin-bottom: 8px;
    padding-top: 6px;
  }

  .token-panel {
    grid-template-columns: minmax(120px, 180px) minmax(280px, 1fr);
    grid-template-areas:
      "status tokens"
      "actions actions";
    gap: 6px 8px;
    padding: 8px;
  }

  .map-status span {
    max-width: 170px;
  }

  .map-actions {
    justify-content: flex-start;
    margin-left: 0;
  }

  .action-group {
    min-height: 32px;
  }
}

@media (max-width: 620px) {
  .topbar,
  .control-panel {
    grid-template-columns: 1fr;
  }

  .cycle-tabs {
    justify-content: flex-start;
  }

  .control-panel > label {
    max-width: 180px;
  }

  .token-panel {
    grid-template-columns: 1fr;
    grid-template-areas:
      "status"
      "tokens"
      "actions";
  }

  .map-status span {
    max-width: none;
  }

  .token-palette {
    flex-wrap: wrap;
  }

  .token-submenu {
    left: auto;
    right: 0;
  }
}
