:root {
  color-scheme: dark;
  --ink: #e8edf2;
  --muted: #9aa7b4;
  --panel: #12171d;
  --panel-2: #192029;
  --line: #2b3540;
  --accent: #e2a84a;
  --accent-2: #66b3a6;
  --danger: #e36a6a;
  --topbar-height: 58px;
  --touchbar-height: 62px;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body {
  min-height: 100vh;
  min-height: 100svh;
  min-height: 100dvh;
  font: 14px/1.45 Inter, ui-sans-serif, system-ui, sans-serif;
  color: var(--ink);
  background: #0b0f13;
}
button, input, select { font: inherit; color: inherit; }
button {
  min-width: 34px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #222b35;
  padding: 7px 10px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); background: #2b3541; }
button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible, #map-canvas:focus-visible {
  outline: 3px solid #f3bb5f;
  outline-offset: 2px;
}
input, select { border: 1px solid var(--line); border-radius: 6px; background: #0d1217; padding: 7px 8px; }
input:focus, select:focus { border-color: var(--accent); }
.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

.topbar {
  position: relative;
  z-index: 50;
  height: var(--topbar-height);
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 8px 14px;
  padding-left: max(14px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 9px; min-width: 215px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border-radius: 9px;
  font: 700 20px Georgia, serif;
  color: #171109;
  background: linear-gradient(145deg, #efbd65, #a96d22);
}
.brand strong, .brand small { display: block; }
.brand small { color: var(--muted); font-size: 11px; letter-spacing: .08em; text-transform: uppercase; }
.portal-link { color: #c8d6df; font-size: 12px; font-weight: 700; text-decoration: none; white-space: nowrap; }
.portal-link:hover { color: #fff; text-decoration: underline; }
.goto-form { display: flex; align-items: center; gap: 8px; }
.goto-form label { color: var(--muted); }
.goto-form input { width: 76px; }
.toolbar-actions { display: flex; gap: 6px; margin-left: auto; }
.toolbar-actions button > small { display: none; }
.toolbar-actions .mobile-only { display: none; }

.workspace {
  height: calc(100vh - var(--topbar-height));
  height: calc(100svh - var(--topbar-height));
  height: calc(100dvh - var(--topbar-height));
  display: grid;
  grid-template-columns: 250px minmax(300px, 1fr) 310px;
}
.sidebar, .inspector {
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  background: var(--panel);
}
.sidebar { border-right: 1px solid var(--line); }
.inspector { border-left: 1px solid var(--line); padding: 14px; }
.sidebar section { padding: 14px; }
.sidebar section + section { border-top: 1px solid var(--line); }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 14px 0; }
.sidebar > .panel-heading { display: none; }
.inspector > .panel-heading { padding: 0; }
.panel-close { display: none; width: 44px; height: 44px; padding: 0; font-size: 25px; line-height: 1; }
h2 { margin: 0 0 9px; font-size: 13px; letter-spacing: .08em; text-transform: uppercase; color: #c9d3dd; }
.panel-heading h2 { margin: 0; }
.search-form { display: flex; gap: 6px; }
.search-form input { min-width: 0; flex: 1; }
.search-form button { padding-inline: 8px; }
.hint { margin: 7px 0; color: var(--muted); font-size: 11px; }
.filter-status { margin: 8px 0; padding: 6px 8px; border: 1px solid #3e755f; border-radius: 6px; color: #bfe4d1; background: #183128; font-size: 11px; }
code { color: #e3b96e; }
.layers { display: grid; grid-template-columns: 1fr 1fr; gap: 7px 9px; }
.layers label { display: flex; align-items: center; gap: 6px; color: #c9d2dc; font-size: 12px; }
.legend p { margin: 6px 0; color: var(--muted); font-size: 12px; }
.dot { display: inline-block; width: 8px; height: 8px; margin-right: 6px; border-radius: 50%; }
.dot.exact { background: #66b3a6; }
.dot.linked { background: #d49a3a; }
.dot.inferred { background: #b47bd3; }
.mobile-goto-form { display: none; }

.map-shell { position: relative; min-width: 0; min-height: 0; overflow: hidden; overscroll-behavior: contain; background: #080b0e; }
#map-canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
  image-rendering: auto;
  touch-action: none;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}
#map-canvas.dragging { cursor: grabbing; }
#map-canvas.pinching { cursor: zoom-in; }
.loading, .map-error {
  position: absolute;
  left: 50%;
  top: 18px;
  transform: translateX(-50%);
  z-index: 2;
  max-width: calc(100% - 24px);
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #111820ee;
  box-shadow: 0 6px 28px #0008;
}
.map-error { color: #ffd0d0; border-color: #823f3f; }
.map-status {
  position: absolute;
  left: 9px;
  right: 9px;
  bottom: 8px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 8px;
  border: 1px solid #2c3743cc;
  border-radius: 5px;
  color: #bac6d1;
  background: #0b1015dd;
  pointer-events: none;
  font: 12px ui-monospace, SFMono-Regular, Consolas, monospace;
}

.search-results { max-height: 250px; overflow: auto; }
.result { width: 100%; min-height: 44px; display: block; margin: 5px 0; padding: 8px; text-align: left; background: var(--panel-2); }
.result strong, .result small { display: block; }
.result small { color: var(--muted); }
.inspector-content { color: #c9d3dd; padding-top: 14px; }
.inspector-content p { color: var(--muted); }
.inspector-content dl { display: grid; grid-template-columns: 94px 1fr; gap: 5px 8px; margin: 0; }
.inspector-content dt { color: var(--muted); }
.inspector-content dd { margin: 0; overflow-wrap: anywhere; }
.item-stack { margin: 12px 0 0; padding: 0; list-style: none; counter-reset: stack -1; }
.item-stack > li { margin: 5px 0; padding: 7px; border-left: 3px solid #4b5967; background: #192029; }
.item-stack strong { color: #f0c477; }
.item-children { margin: 5px 0 0 16px; color: var(--muted); }
.panel-backdrop { display: none; }

@media (max-width: 1100px) {
  :root { --touchbar-height: 64px; }
  .topbar { gap: 12px; }
  .goto-form { display: none; }
  .toolbar-actions {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 60;
    height: calc(var(--touchbar-height) + env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: repeat(8, minmax(40px, 1fr));
    gap: 4px;
    margin: 0;
    padding: 5px max(6px, env(safe-area-inset-right)) max(5px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    border-top: 1px solid var(--line);
    background: #12171df5;
    backdrop-filter: blur(10px);
  }
  .toolbar-actions .mobile-only { display: flex; }
  .toolbar-actions button {
    min-width: 40px;
    min-height: 50px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1px;
    padding: 3px 2px;
    border-color: transparent;
    background: transparent;
    font-size: 19px;
  }
  .toolbar-actions button[aria-expanded="true"] { color: #fff0c5; border-color: #a87831; background: #3a2d1b; }
  .toolbar-actions button > small { display: block; color: var(--muted); font-size: 9px; line-height: 1; }
  .workspace { position: relative; display: block; }
  .map-shell { width: 100%; height: 100%; }
  .sidebar, .inspector {
    position: fixed;
    top: var(--topbar-height);
    bottom: calc(var(--touchbar-height) + env(safe-area-inset-bottom));
    z-index: 55;
    width: min(82vw, 380px);
    max-width: 100%;
    display: block;
    visibility: hidden;
    box-shadow: 0 10px 34px #000c;
    transition: transform .2s ease, visibility 0s linear .2s;
  }
  .sidebar { left: 0; transform: translateX(-105%); border-right: 1px solid var(--line); }
  .inspector { right: 0; transform: translateX(105%); border-left: 1px solid var(--line); }
  .sidebar.mobile-open, .inspector.mobile-open {
    visibility: visible;
    transform: translateX(0);
    transition: transform .2s ease;
  }
  .sidebar > .panel-heading, .panel-close { display: flex; }
  .sidebar[data-mobile-section="search"] section[data-mobile-section="layers"],
  .sidebar[data-mobile-section="layers"] section[data-mobile-section="search"] { display: none; }
  .sidebar section { padding: 16px; }
  .layers { grid-template-columns: 1fr; gap: 2px; }
  .layers label { min-height: 44px; font-size: 14px; }
  .layers input { width: 20px; height: 20px; }
  .search-form input, .search-form button, .mobile-goto-form input, .mobile-goto-form select, .mobile-goto-form button { min-height: 44px; }
  .mobile-goto-form { display: grid; grid-template-columns: 1fr 1fr 70px 48px; gap: 6px; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
  .mobile-goto-form label { display: grid; gap: 3px; color: var(--muted); font-size: 11px; }
  .mobile-goto-form input, .mobile-goto-form select { width: 100%; min-width: 0; }
  .mobile-goto-form button { align-self: end; }
  .map-status { right: 8px; bottom: calc(var(--touchbar-height) + env(safe-area-inset-bottom) + 6px); justify-content: center; }
  #cursor-position, #cache-status { display: none; }
  .panel-backdrop:not([hidden]) {
    position: fixed;
    inset: var(--topbar-height) 0 calc(var(--touchbar-height) + env(safe-area-inset-bottom));
    z-index: 54;
    display: block;
    width: 100%;
    border: 0;
    border-radius: 0;
    background: #0306099c;
    backdrop-filter: blur(1px);
  }
}

@media (max-width: 699px) {
  :root { --topbar-height: 54px; --touchbar-height: 60px; }
  .topbar { gap: 8px; padding-top: max(7px, env(safe-area-inset-top)); padding-bottom: 7px; }
  .brand { min-width: 0; gap: 7px; overflow: hidden; }
  .brand-mark { width: 32px; height: 32px; font-size: 18px; }
  .brand strong { overflow: hidden; font-size: 13px; white-space: nowrap; text-overflow: ellipsis; }
  .brand small { display: none; }
  .portal-link { margin-left: auto; padding: 9px 4px; }
  .toolbar-actions { gap: 1px; padding-inline: max(2px, env(safe-area-inset-left)); }
  .toolbar-actions button { min-width: 0; border-radius: 6px; font-size: 17px; }
  .toolbar-actions button > small { font-size: 8px; }
  .sidebar, .inspector {
    top: auto;
    left: 0;
    right: 0;
    bottom: calc(var(--touchbar-height) + env(safe-area-inset-bottom));
    width: 100%;
    max-height: min(68vh, 620px);
    max-height: min(68dvh, 620px);
    border: 1px solid var(--line);
    border-bottom: 0;
    border-radius: 18px 18px 0 0;
    transform: translateY(105%);
  }
  .sidebar.mobile-open, .inspector.mobile-open { transform: translateY(0); }
  .panel-heading { position: sticky; top: 0; z-index: 2; padding: 10px 12px; border-bottom: 1px solid var(--line); background: #12171df5; }
  .inspector { padding: 0 14px 18px; }
  .inspector > .panel-heading { margin: 0 -14px; padding-inline: 14px; }
  .search-form { display: grid; grid-template-columns: 1fr auto; }
  .search-form #search-clear { grid-column: 1 / -1; }
  .mobile-goto-form { grid-template-columns: 1fr 1fr 62px; }
  .mobile-goto-form button { grid-column: 1 / -1; }
  .map-status { left: 6px; right: 6px; padding-inline: 5px; font-size: 10px; }
}

@media (max-width: 430px) {
  .inspector-content dl { display: block; }
  .inspector-content dt { margin-top: 10px; font-size: 11px; }
  .inspector-content dd { padding-bottom: 2px; }
}

@media (max-height: 500px) and (max-width: 1100px) and (orientation: landscape) {
  :root { --topbar-height: 48px; --touchbar-height: 54px; }
  .topbar { padding-top: 5px; padding-bottom: 5px; }
  .brand { min-width: 0; }
  .brand-mark { width: 30px; height: 30px; }
  .brand strong { font-size: 13px; white-space: nowrap; }
  .brand small { display: none; }
  .sidebar, .inspector {
    top: var(--topbar-height);
    bottom: calc(var(--touchbar-height) + env(safe-area-inset-bottom));
    width: min(72vw, 380px);
    max-height: none;
    border-radius: 0;
  }
  .sidebar { right: auto; transform: translateX(-105%); }
  .inspector { left: auto; transform: translateX(105%); }
  .sidebar.mobile-open, .inspector.mobile-open { transform: translateX(0); }
  .toolbar-actions button { min-height: 44px; }
}

@media (hover: none) and (pointer: coarse) {
  button, input, select { min-height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
