:root {
  --bg-desk: #19140f;
  --bg-panel: #241c15;
  --bg-panel-elev: #2f251c;
  --bg-input: #1b1510;
  --border-wood: #473828;
  --border-gold: #8c6d3b;
  --gold: #d6b164;
  --gold-bright: #f0d38c;
  --parchment: #f0e3c6;
  --text-main: #efe4ce;
  --text-muted: #b6a68c;
  --danger: #b85346;
  --accent-btn: #5f4222;
  --font-serif: "Yu Mincho", "Hiragino Mincho ProN", "Noto Serif JP", Georgia, serif;
  --font-sans: "Yu Gothic UI", "Hiragino Kaku Gothic ProN", "Meiryo", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg-desk);
  color: var(--text-main);
  font-family: var(--font-sans);
  font-size: 13px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

input[type="text"],
textarea {
  user-select: text;
  -webkit-user-select: text;
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}

/* ===== TOP BAR ===== */
.topbar {
  height: 54px;
  background: linear-gradient(180deg, #2b2219 0%, #1f1812 100%);
  border-bottom: 1px solid var(--border-wood);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  gap: 12px;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.brand-emblem {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--gold);
  background: radial-gradient(circle at 35% 35%, #5c4523, #24190d);
  color: var(--gold-bright);
  display: grid;
  place-items: center;
  font-size: 16px;
  box-shadow: 0 0 12px rgba(214, 177, 100, 0.25);
}

.brand-text h1 {
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--parchment);
  line-height: 1.15;
}

.brand-sub {
  font-size: 9px;
  letter-spacing: 0.2em;
  color: var(--gold);
  display: block;
}

.topbar-center {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  justify-content: center;
  max-width: 520px;
}

.world-title-wrap {
  display: flex;
  align-items: center;
  background: var(--bg-input);
  border: 1px solid var(--border-wood);
  border-radius: 6px;
  padding: 2px 8px 2px 6px;
  flex: 1;
  max-width: 280px;
  transition: border-color 0.15s;
}

.world-title-wrap:focus-within {
  border-color: var(--gold);
}

.field-tag {
  font-size: 10px;
  color: var(--gold);
  padding: 2px 6px;
  border-right: 1px solid var(--border-wood);
  margin-right: 8px;
  white-space: nowrap;
}

.world-title-wrap input {
  background: transparent;
  border: none;
  color: var(--parchment);
  font-family: var(--font-serif);
  font-size: 14px;
  width: 100%;
  outline: none;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.btn-group {
  display: inline-flex;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border-wood);
}

.btn-group .btn {
  border: none;
  border-radius: 0;
  border-right: 1px solid var(--border-wood);
}

.btn-group .btn:last-child {
  border-right: none;
}

/* ===== BUTTONS ===== */
.btn {
  background: var(--bg-panel-elev);
  color: var(--text-main);
  border: 1px solid var(--border-wood);
  border-radius: 6px;
  padding: 6px 11px;
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  transition: background 0.14s, border-color 0.14s, transform 0.08s;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: #3d3024;
  border-color: var(--border-gold);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-icon-only {
  min-width: 30px;
  padding: 6px 8px;
  font-size: 14px;
}

.btn-zoom-label {
  min-width: 52px;
  font-variant-numeric: tabular-nums;
}

.btn-gold {
  background: linear-gradient(180deg, #6e5227 0%, #4e3819 100%);
  border-color: var(--gold);
  color: #fff6df;
  font-weight: 600;
}

.btn-gold:hover:not(:disabled) {
  background: linear-gradient(180deg, #846331 0%, #5e441f 100%);
  border-color: var(--gold-bright);
}

.btn-primary {
  background: linear-gradient(180deg, #3c5c4a 0%, #2a4335 100%);
  border-color: #66947a;
  color: #f3f9f5;
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #49705a 0%, #335241 100%);
  border-color: #84b89b;
}

.btn-danger {
  border-color: #773a32;
  color: #f2b7ae;
}

.btn-danger:hover:not(:disabled) {
  background: #4e2520;
  border-color: var(--danger);
}

.btn-sm {
  padding: 5px 8px;
  font-size: 11px;
}

.btn-block {
  width: 100%;
  padding: 9px 12px;
  font-size: 13px;
  margin-top: 8px;
}

.btn-link {
  background: none;
  border: none;
  color: var(--gold);
  font-size: 11px;
  cursor: pointer;
  text-decoration: underline;
}

.btn-link:hover {
  color: var(--gold-bright);
}

/* ===== MAIN WORKSPACE ===== */
.workspace {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* ===== SIDEBAR ===== */
.sidebar {
  width: 304px;
  flex-shrink: 0;
  background: var(--bg-panel);
  border-right: 1px solid var(--border-wood);
  display: flex;
  flex-direction: column;
  z-index: 10;
}

.tab-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: #1c150f;
  border-bottom: 1px solid var(--border-wood);
}

.tab-btn {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  padding: 10px 2px;
  font-size: 11.5px;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
}

.tab-btn:hover {
  color: var(--text-main);
  background: rgba(214, 177, 100, 0.06);
}

.tab-btn.active {
  color: var(--gold-bright);
  border-bottom-color: var(--gold);
  background: rgba(214, 177, 100, 0.1);
  font-weight: 600;
}

.sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.sidebar-body::-webkit-scrollbar {
  width: 6px;
}

.sidebar-body::-webkit-scrollbar-thumb {
  background: #4b3b2b;
  border-radius: 3px;
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.panel-section {
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid rgba(214, 177, 100, 0.14);
}

.panel-section:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

.section-title {
  font-family: var(--font-serif);
  font-size: 12px;
  letter-spacing: 0.08em;
  color: var(--gold);
  margin-bottom: 9px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.section-title::before {
  content: "◆";
  font-size: 8px;
  opacity: 0.8;
}

/* Tool Buttons */
.tool-grid {
  display: grid;
  gap: 6px;
}

.tool-grid-2 {
  grid-template-columns: repeat(2, 1fr);
}

.tool-grid-3 {
  grid-template-columns: repeat(3, 1fr);
}

.tool-btn {
  background: var(--bg-panel-elev);
  border: 1px solid var(--border-wood);
  border-radius: 6px;
  padding: 7px 8px;
  color: var(--text-main);
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
  transition: all 0.14s;
}

.tool-btn:hover {
  border-color: var(--border-gold);
  background: #3a2e22;
}

.tool-btn.active {
  background: linear-gradient(180deg, #4b3923 0%, #382a19 100%);
  border-color: var(--gold-bright);
  box-shadow: inset 0 0 0 1px rgba(240, 211, 140, 0.3);
}

.tool-btn.compact {
  justify-content: center;
  padding: 7px 4px;
  font-size: 11px;
}

.tool-label {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.tool-label strong {
  font-size: 12px;
  font-weight: 600;
  line-height: 1.2;
}

.tool-label small {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.swatch {
  width: 22px;
  height: 22px;
  border-radius: 5px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  flex-shrink: 0;
}

.swatch-land { background: linear-gradient(135deg, #dfcca2, #b89c68); }
.swatch-sea { background: linear-gradient(135deg, #8aa9a8, #587577); }
.swatch-smooth { background: linear-gradient(135deg, #c7bca2, #8e836e); }
.swatch-river { background: linear-gradient(135deg, #6ba2b0, #3b6d80); }
.swatch-ink-black { background: linear-gradient(135deg, #332d29, #100e0c); border-color: rgba(240, 211, 140, 0.55); }
.swatch-ink-erase { background: linear-gradient(135deg, #efe4ce, #c2b294); border-style: dashed; }
.swatch-plains { background: linear-gradient(135deg, #d7cfa1, #b9b57d); }
.swatch-forest { background: linear-gradient(135deg, #8fa372, #63794e); }
.swatch-dense { background: linear-gradient(135deg, #58704c, #374c31); }
.swatch-mountain { background: linear-gradient(135deg, #b8a386, #7a654e); }
.swatch-desert { background: linear-gradient(135deg, #e7ca88, #c79f59); }
.swatch-snow { background: linear-gradient(135deg, #f3f1e8, #cfd4d6); }
.swatch-swamp { background: linear-gradient(135deg, #8a8f6a, #5c6244); }
.swatch-volcano { background: linear-gradient(135deg, #744943, #3b2426); }
.swatch-sketch-land { background: linear-gradient(135deg, #d89b48, #9c6222); border-style: dashed; }
.swatch-sketch-erase { background: linear-gradient(135deg, #6495a8, #355868); border-style: dashed; }
.swatch-sketch-mtn { background: linear-gradient(135deg, #d96348, #8c2d1f); border-style: dashed; }

.helper-note {
  font-size: 11px;
  line-height: 1.45;
  color: var(--text-muted);
  background: rgba(0, 0, 0, 0.22);
  border-left: 2px solid var(--gold);
  padding: 6px 8px;
  border-radius: 0 4px 4px 0;
}

.icon-badge {
  width: 22px;
  height: 22px;
  border-radius: 5px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(214, 177, 100, 0.28);
  display: grid;
  place-items: center;
  font-size: 13px;
  flex-shrink: 0;
}

/* Preset Grid */
.preset-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.preset-btn {
  background: var(--bg-panel-elev);
  border: 1px solid var(--border-wood);
  border-radius: 6px;
  padding: 8px;
  color: var(--text-main);
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: all 0.14s;
}

.preset-btn:hover {
  border-color: var(--border-gold);
  background: #3a2e22;
}

.preset-btn.active {
  background: linear-gradient(180deg, #4b3923 0%, #382a19 100%);
  border-color: var(--gold-bright);
}

.preset-btn strong {
  font-size: 12px;
}

.preset-btn small {
  font-size: 10px;
  color: var(--text-muted);
  line-height: 1.25;
}

.preset-btn-wide {
  grid-column: 1 / -1;
}

.country-options {
  margin-top: 9px;
  padding: 9px 10px;
  background: rgba(20, 15, 10, 0.55);
  border: 1px solid var(--border-gold);
  border-radius: 6px;
}

.country-options[hidden] {
  display: none;
}

.country-pan-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.country-pan-grid .control-row {
  margin-bottom: 8px;
}

/* Form Controls */
.control-row {
  margin-bottom: 11px;
}

.control-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
  font-size: 12px;
  color: var(--text-main);
}

.val-badge {
  font-size: 11px;
  color: var(--gold-bright);
  font-variant-numeric: tabular-nums;
  background: rgba(0, 0, 0, 0.3);
  padding: 1px 6px;
  border-radius: 4px;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--gold);
  cursor: pointer;
}

.text-input,
.select-input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border-wood);
  border-radius: 5px;
  color: var(--parchment);
  padding: 6px 8px;
  font-size: 12px;
  font-family: inherit;
  outline: none;
}

.text-input:focus,
.select-input:focus {
  border-color: var(--gold);
}

.marker-name-row {
  display: flex;
  gap: 5px;
  margin-bottom: 8px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 7px;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-main);
}

.checkbox-row input {
  accent-color: var(--gold);
  cursor: pointer;
}

.decor-toggles {
  margin-top: 4px;
}

.action-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.action-row .btn {
  flex: 1;
}

.sidebar-footer {
  background: #1a140e;
  border-top: 1px solid var(--border-wood);
  padding: 7px 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  font-size: 10px;
  color: var(--text-muted);
}

/* ===== CANVAS STAGE ===== */
.btn-pen-toggle.active {
  background: linear-gradient(180deg, #5d4620 0%, #3d2c12 100%);
  border-color: var(--gold-bright);
  color: #fff6dc;
  box-shadow: 0 0 10px rgba(214, 177, 100, 0.32);
  font-weight: 600;
}

.sidebar.collapsed {
  display: none;
}

.canvas-stage {
  flex: 1;
  position: relative;
  overflow: hidden;
  touch-action: none;
  background:
    radial-gradient(circle at center, #2b2219 0%, #15100c 100%);
  display: grid;
  place-items: center;
}

.canvas-wrap {
  position: relative;
  transform-origin: center center;
  touch-action: none;
  box-shadow:
    0 20px 60px rgba(0, 0, 0, 0.72),
    0 0 0 1px rgba(214, 177, 100, 0.25);
  border-radius: 2px;
  cursor: crosshair;
}

.canvas-wrap.panning {
  cursor: grabbing;
}

.canvas-wrap.select-mode {
  cursor: default;
}

#mapCanvas,
#overlayCanvas {
  display: block;
  width: 1440px;
  height: 900px;
  touch-action: none;
}

#overlayCanvas {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.status-pill {
  position: absolute;
  bottom: 14px;
  left: 16px;
  background: rgba(24, 18, 13, 0.88);
  border: 1px solid var(--border-wood);
  backdrop-filter: blur(6px);
  border-radius: 999px;
  padding: 5px 14px;
  font-size: 11px;
  color: var(--parchment);
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.status-pill .sep {
  color: var(--border-gold);
  opacity: 0.6;
}

.toast {
  position: absolute;
  top: 16px;
  right: 18px;
  background: rgba(34, 26, 18, 0.95);
  border: 1px solid var(--gold);
  color: var(--gold-bright);
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1120px) {
  .brand-sub,
  .brand-emblem { display: none; }
  .topbar { padding: 0 10px; gap: 8px; }
  .brand-text h1 { font-size: 14px; }
  .world-title-wrap { max-width: 185px; }
  .field-tag { display: none; }
  .sidebar { width: 282px; }
}

@media (max-width: 860px) {
  .brand-text { display: none; }
  .btn-text-hide-sm { display: none; }
  .zoom-group { display: none; }
  .workspace { position: relative; }
  .sidebar {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 296px;
    max-width: 84vw;
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.65);
    z-index: 30;
  }
}

/* Touch / iPad / Apple Pencil ergonomic sizing */
@media (pointer: coarse) {
  .btn {
    min-height: 36px;
    padding: 7px 11px;
  }
  .btn-icon-only {
    min-width: 36px;
    font-size: 16px;
  }
  .tab-btn {
    padding: 12px 4px;
    font-size: 12.5px;
  }
  .tool-btn,
  .preset-btn {
    min-height: 44px;
    padding: 9px 10px;
  }
  .checkbox-row {
    min-height: 32px;
    padding: 2px 0;
  }
  .checkbox-row input[type="checkbox"] {
    width: 18px;
    height: 18px;
  }
  .select-input,
  .text-input {
    min-height: 36px;
    font-size: 14px;
  }
  input[type="range"] {
    height: 28px;
  }
  .sidebar-body {
    -webkit-overflow-scrolling: touch;
  }
}
