:root {
  --bg: #0f1420;
  --panel: rgba(17, 23, 36, 0.94);
  --card: #1a2233;
  --card-2: #212b40;
  --line: #2c3854;
  --text: #eef2fb;
  --muted: #9aa7c2;
  --red: #e52521;
  --red-2: #ff4a3d;
  --yellow: #ffd21f;
  --blue: #2a7cf6;
  --green: #34c759;
  --warn: #ffb020;
  --bad: #ff5a4f;
  --radius: 14px;
  --panel-w: 380px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 var(--font); }
button, input, select { font: inherit; color: inherit; }
a { color: #8fb8ff; }
kbd { background: #fff2; border-radius: 4px; padding: 0 5px; font-size: 0.9em; }

#app { position: fixed; inset: 0; overflow: hidden; }
#map { position: absolute; inset: 0; right: var(--panel-w); background: #0b0f18; }
.leaflet-container { font: 12px/1.4 var(--font); background: #0b0f18; }
.leaflet-control-attribution { background: rgba(10, 14, 24, 0.7) !important; color: #c8d1e6 !important; }
.leaflet-control-attribution a { color: #9cc0ff !important; }
.leaflet-control-layers { background: var(--panel) !important; color: var(--text); border: 1px solid var(--line) !important; border-radius: 10px !important; }
.leaflet-control-layers-expanded { padding: 8px 12px !important; }
.leaflet-bar a { background: var(--panel) !important; color: var(--text) !important; border-color: var(--line) !important; }

/* ---------------- top bar: brand + search */
#topbar {
  position: absolute; top: calc(12px + env(safe-area-inset-top, 0px)); left: 12px; right: calc(var(--panel-w) + 12px);
  display: flex; gap: 10px; align-items: center; z-index: 1000; pointer-events: none;
}
#topbar > * { pointer-events: auto; }
.brand {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 8px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line); color: var(--text); text-decoration: none;
  font-weight: 800; letter-spacing: 0.2px; font-size: 17px; box-shadow: 0 6px 24px #0007;
}
#search { position: relative; flex: 1; max-width: 460px; display: flex; }
#search input {
  flex: 1; min-width: 0; height: 42px; padding: 0 44px 0 16px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--panel); box-shadow: 0 6px 24px #0007; outline: none;
}
#search input:focus { border-color: var(--blue); }
#search button { position: absolute; right: 4px; top: 4px; width: 34px; height: 34px; border-radius: 50%; border: 0; background: var(--blue); color: #fff; display: grid; place-items: center; cursor: pointer; }
#search-results {
  position: absolute; top: 48px; left: 0; right: 0; margin: 0; padding: 6px; list-style: none; background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 10px 30px #000a; max-height: 50vh; overflow: auto;
}
#search-results li { padding: 8px 10px; border-radius: 8px; cursor: pointer; }
#search-results li:hover, #search-results li.active { background: var(--card-2); }
#search-results li small { display: block; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------------- map tools */
#map-tools { position: absolute; left: 12px; top: calc(66px + env(safe-area-inset-top, 0px)); z-index: 1000; display: flex; gap: 8px; }
.tool {
  display: flex; align-items: center; gap: 8px; height: 40px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel); cursor: pointer; box-shadow: 0 6px 20px #0006; font-weight: 600;
}
.tool:hover { border-color: #4a5a82; }
.tool.active { background: var(--yellow); color: #1b1400; border-color: var(--yellow); }
#draw-hint {
  position: absolute; left: 50%; transform: translateX(-50%); right: auto; top: calc(66px + env(safe-area-inset-top, 0px)); margin-left: calc(var(--panel-w) / -2);
  z-index: 1000; background: var(--yellow); color: #1b1400; padding: 8px 14px; border-radius: 999px; font-weight: 600; box-shadow: 0 6px 20px #0006;
}
#map.drawing, #map.drawing .leaflet-interactive { cursor: crosshair !important; }

/* rectangle handles + label */
.e64-handle { background: #fff; border: 3px solid var(--red); border-radius: 50%; box-shadow: 0 1px 6px #000a; }
.e64-handle.move { background: var(--red); border-color: #fff; border-radius: 8px; display: grid; place-items: center; }
.e64-handle.move::after { content: ""; width: 12px; height: 12px; background: no-repeat center/contain url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2l4 4h-3v5h5V8l4 4-4 4v-3h-5v5h3l-4 4-4-4h3v-5H6v3l-4-4 4-4v3h5V6H8z' fill='%23fff'/%3E%3C/svg%3E"); }
.e64-label { width: max-content;
  background: rgba(15, 20, 32, 0.9); color: #fff; border: 1px solid #fff3; border-radius: 8px; padding: 2px 8px; white-space: nowrap;
  font: 600 12px/1.6 var(--font); box-shadow: 0 2px 8px #0008; transform: translate(-50%, -140%);
}

/* ---------------- panel */
#panel {
  position: absolute; top: 0; right: 0; bottom: 0; width: var(--panel-w); background: var(--panel); border-left: 1px solid var(--line);
  z-index: 1100; display: flex; flex-direction: column; backdrop-filter: blur(8px);
}
#panel-handle { display: none; }
.panel-scroll { overflow-y: auto; padding: calc(16px + env(safe-area-inset-top, 0px)) 16px calc(16px + env(safe-area-inset-bottom, 0px)); }
.intro h1 { font-size: 21px; line-height: 1.2; margin: 4px 0 8px; letter-spacing: -0.2px; }
.intro p { color: var(--muted); margin: 0 0 14px; font-size: 14px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.card h2 { font-size: 15px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
.num { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%; background: var(--red); color: #fff; font-size: 12px; font-weight: 800; }
.area-size { font-size: 20px; font-weight: 800; letter-spacing: -0.2px; }
.area-scale { color: var(--muted); font-size: 13px; margin-top: 2px; }
.warning { margin-top: 10px; padding: 9px 11px; border-radius: 10px; background: #ffb0201a; border: 1px solid #ffb02066; color: #ffd58a; font-size: 13px; }
.warning.bad { background: #ff5a4f1a; border-color: #ff5a4f77; color: #ffb3ad; }
.field { display: grid; grid-template-columns: 64px 1fr; align-items: center; gap: 10px; margin: 8px 0; }
.field > span { color: var(--muted); font-size: 13px; }
.field input, .field select { height: 36px; padding: 0 10px; border-radius: 9px; border: 1px solid var(--line); background: var(--card-2); outline: none; min-width: 0; width: 100%; }
.field input:focus, .field select:focus { border-color: var(--blue); }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--card-2); border: 1px solid var(--line); border-radius: 9px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: transparent; border-radius: 7px; height: 30px; cursor: pointer; font-size: 13px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--blue); color: #fff; }
.toggles { display: flex; flex-wrap: wrap; gap: 8px 18px; margin: 10px 0; }
.toggle { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; user-select: none; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.sw { width: 36px; height: 20px; border-radius: 999px; background: #3a4560; position: relative; transition: background .15s; flex: none; }
.sw::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.toggle input:checked + .sw { background: var(--green); }
.toggle input:checked + .sw::after { transform: translateX(16px); }
.toggle input:focus-visible + .sw { outline: 2px solid var(--blue); outline-offset: 2px; }

.drop {
  display: flex; gap: 12px; align-items: center; padding: 14px; border-radius: 12px; border: 2px dashed #4a5a82; cursor: pointer;
  background: #ffffff06; transition: background .15s, border-color .15s;
}
.drop:hover, .drop.over { border-color: var(--yellow); background: #ffd21f12; }
.drop-icon { color: var(--yellow); flex: none; }
.drop-text { font-size: 14px; }
.drop-text span { color: var(--muted); font-size: 13px; }
.rom-ok { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; background: #34c75914; border: 1px solid #34c75955; }
.rom-ok > div { flex: 1; font-size: 14px; }
.rom-ok span { color: var(--muted); font-size: 12px; }
.fine { color: var(--muted); font-size: 12px; margin: 10px 0 0; }
.link { background: none; border: 0; color: #8fb8ff; cursor: pointer; padding: 0; text-decoration: underline; }
.link.small { display: block; text-align: center; margin-top: 8px; font-size: 13px; }

.go-row { display: grid; grid-template-columns: 1fr 1.6fr; gap: 10px; }
.primary, .secondary {
  height: 48px; border-radius: 12px; border: 0; cursor: pointer; font-weight: 800; font-size: 16px; letter-spacing: 0.2px;
  display: flex; align-items: center; justify-content: center; text-decoration: none;
}
.primary { background: linear-gradient(180deg, var(--red-2), var(--red)); color: #fff; box-shadow: 0 4px 0 #9c1410, 0 8px 20px #e5252155; }
.primary:active { transform: translateY(2px); box-shadow: 0 2px 0 #9c1410; }
.primary:disabled { filter: grayscale(0.8) brightness(0.7); cursor: not-allowed; box-shadow: none; }
.primary.big { margin-top: 12px; background: linear-gradient(180deg, #ffe066, var(--yellow)); color: #2a1d00; box-shadow: 0 4px 0 #b8900b, 0 8px 20px #ffd21f44; }
.secondary { background: var(--card-2); color: var(--text); border: 1px solid var(--line); font-weight: 700; font-size: 15px; }
.secondary:hover { border-color: #4a5a82; }
.secondary:disabled { opacity: 0.5; cursor: not-allowed; }

#steps { list-style: none; margin: 14px 0 0; padding: 0; }
#steps li { display: grid; grid-template-columns: 22px 1fr; gap: 8px; align-items: start; padding: 5px 0; font-size: 14px; color: var(--muted); }
#steps li .ic { width: 18px; height: 18px; border-radius: 50%; border: 2px solid #3a4560; margin-top: 1px; }
#steps li.run { color: var(--text); }
#steps li.run .ic { border-color: var(--yellow); border-top-color: transparent; animation: spin 0.8s linear infinite; }
#steps li.done { color: var(--text); }
#steps li.done .ic { border: 0; background: var(--green) no-repeat center/12px url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='m4 12.5 5 5L20 6.5' fill='none' stroke='%23fff' stroke-width='4' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); }
#steps li.fail { color: var(--bad); }
#steps li.fail .ic { border-color: var(--bad); background: var(--bad); }
#steps li small { display: block; color: var(--muted); font-size: 12px; }
#steps .bar { height: 3px; background: #2c3854; border-radius: 2px; margin-top: 4px; overflow: hidden; }
#steps .bar i { display: block; height: 100%; background: var(--yellow); width: 0; transition: width .2s; }
@keyframes spin { to { transform: rotate(360deg); } }

#result { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }
.result-head { display: flex; gap: 10px; align-items: center; }
.result-head span { color: var(--muted); font-size: 13px; }

.foot { padding: 4px 2px 10px; }
.foot p { margin: 4px 0; font-size: 13px; color: var(--muted); }

/* ---------------- 3D preview */
#preview { position: absolute; inset: 0; right: var(--panel-w); z-index: 1050; background: #9ec9f0; }
#preview-canvas { position: absolute; inset: 0; }
#preview-canvas canvas { width: 100%; height: 100%; display: block; touch-action: none; }
.preview-bar {
  position: absolute; left: 12px; right: 12px; top: calc(12px + env(safe-area-inset-top, 0px)); display: flex; justify-content: space-between; align-items: center; gap: 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 8px 8px 14px; box-shadow: 0 6px 24px #0006;
}
.preview-bar span { color: var(--muted); font-size: 13px; }
.preview-bar .secondary { height: 36px; padding: 0 14px; }
.preview-legend { position: absolute; left: 12px; bottom: calc(28px + env(safe-area-inset-bottom, 0px)); background: var(--panel); border-radius: 10px; padding: 6px 10px; font-size: 12px; display: flex; gap: 8px; align-items: center; }
.lg { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.lg.mario { background: var(--red); }
.lg.car { background: #1e88e5; margin-left: 8px; }

#file-warning { position: fixed; inset: 0; z-index: 5000; background: #000c; display: grid; place-items: center; padding: 20px; }
.fw-box { max-width: 560px; background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 22px; }
.fw-box code { background: #fff1; padding: 1px 6px; border-radius: 5px; }

/* ---------------- phone layout: bottom sheet */
@media (max-width: 760px) {
  :root { --panel-w: 0px; }
  #map { right: 0; bottom: 38vh; }
  #preview { right: 0; bottom: 38vh; }
  #topbar { right: 12px; }
  .brand span { display: none; }
  #draw-hint { margin-left: 0; top: auto; bottom: calc(38vh + 12px); max-width: 92vw; text-align: center; font-size: 13px; }
  #map-tools { top: calc(62px + env(safe-area-inset-top, 0px)); }
  #panel { top: auto; left: 0; width: auto; height: 38vh; border-left: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; transition: height .25s; }
  #panel.expanded { height: 88vh; }
  #panel-handle { display: block; width: 100%; height: 22px; border: 0; background: transparent; cursor: pointer; flex: none; }
  #panel-handle span { display: block; width: 44px; height: 5px; border-radius: 3px; background: #4a5a82; margin: 8px auto 0; }
  .panel-scroll { padding-top: 4px; }
  .intro h1 { font-size: 18px; }
  .intro p { display: none; }
}
