/* Floor Plan Web Editor — Phase 1 (view + export)
   Light, professional real-estate-tool theme. */

:root {
  --bg:            #f4f5f7;
  --panel:         #ffffff;
  --ink:           #1d2330;
  --ink-soft:      #5b6473;
  --line:          #e2e5ea;
  --line-strong:   #c9ced6;
  --accent:        #2f6fed;
  --accent-ink:    #ffffff;
  --accent-soft:   #eaf1ff;
  --danger:        #d4493a;
  --ok:            #1f9d63;

  /* Plan rendering colors */
  --wall-outer:    #1d2330;
  --wall-inner:    #4a5260;
  --wall-railing:  #8a93a3;
  --opening:       #ffffff;
  --window-pane:   #6db6e8;
  --door-arc:      #9aa3b2;
  --room-fill:     rgba(47,111,237,0.04);
  --room-ink:      #2a3140;
  --area-ink:      #6b7280;
  --label-ink:     #2a3140;

  /* Furniture (editor-only 2D rendering) */
  --furn-fill:     rgba(120,134,156,0.14);
  --furn-stroke:   #6b7686;
  --furn-ink:      #4a5260;
  --furn-tick:     #9aa3b2;
  --furn-name:     #5b6473;

  --shadow: 0 1px 2px rgba(20,30,55,.06), 0 4px 16px rgba(20,30,55,.08);
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
  z-index: 5;
  flex: 0 0 auto;
}

.brand { display: flex; align-items: center; gap: 8px; }
.brand-mark {
  font-size: 18px; color: var(--accent);
  width: 30px; height: 30px; display: grid; place-items: center;
  background: var(--accent-soft); border-radius: 8px;
}
.brand-name { font-weight: 700; letter-spacing: .2px; }

.title-block { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.plan-title {
  font-size: 15px; font-weight: 600; margin: 0;
  max-width: 38vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.plan-subtitle { font-size: 12px; color: var(--ink-soft); }

.spacer { flex: 1 1 auto; }

/* Floating bottom toolbar — out of the top header's flow, centered over the
   canvas. flex-wrap keeps every control reachable on narrow/mobile screens. */
.toolbar {
  position: fixed;
  left: 50%; bottom: 14px;
  transform: translateX(-50%);
  z-index: 30;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center;
  gap: 8px;
  max-width: calc(100vw - 20px);
  padding: 8px 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: 0 10px 34px rgba(15, 23, 42, .20);
}
.tool-group {
  display: flex; align-items: center; gap: 4px;
  padding: 3px; border: 1px solid var(--line); border-radius: 8px;
  background: #fbfcfe;
}
.group-label {
  font-size: 11px; color: var(--ink-soft); padding: 0 6px;
  text-transform: uppercase; letter-spacing: .4px;
}

/* ---------- Buttons ---------- */
.btn {
  font: inherit; font-size: 13px; font-weight: 600;
  border: 1px solid var(--line-strong);
  background: var(--panel); color: var(--ink);
  border-radius: 8px; padding: 7px 14px; cursor: pointer;
  transition: background .12s, border-color .12s, transform .04s;
  user-select: none;
}
.btn:hover  { background: #f0f2f6; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn-icon {
  width: 34px; height: 34px; padding: 0;
  display: grid; place-items: center; font-size: 17px;
  border-color: transparent; background: transparent;
}
.btn-icon:hover { background: #eef1f6; }

.btn-primary {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
}
.btn-primary:hover { background: #2660d8; }
.btn-secondary { background: var(--panel); }

.btn.is-busy { position: relative; color: transparent !important; pointer-events: none; }
.btn.is-busy::after {
  content: ""; position: absolute; inset: 0; margin: auto;
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.5); border-top-color: #fff;
  animation: spin .7s linear infinite;
}
.btn-secondary.is-busy::after { border-color: rgba(0,0,0,.25); border-top-color: var(--accent); }

/* Export file-format picker (pdf/png/jpg) — styled as a secondary button. */
.export-format { cursor: pointer; padding-right: 8px; }

/* ---------- Stage / canvas ---------- */
.stage { position: relative; flex: 1 1 auto; min-height: 0; }
.canvas-wrap {
  position: absolute; inset: 0;
  background:
    linear-gradient(var(--line) 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(90deg, var(--line) 1px, transparent 1px) 0 0 / 24px 24px,
    var(--bg);
  cursor: grab;
  overflow: hidden;
}
.canvas-wrap.dragging { cursor: grabbing; }
/* translateZ(0) promotes the SVG to its own GPU layer so the whole layer
   re-rasterizes on every pan/zoom — without it, transforming #viewport leaves
   repaint trails (ghosted walls / stacked labels) in WebKit & some Chrome. */
.plan-svg { width: 100%; height: 100%; display: block; transform: translateZ(0); }
/* Promote the pan/zoom group too: its transform becomes a compositor op,
   not a re-raster, eliminating the smearing during continuous gestures. */
#viewport { will-change: transform; }

/* SVG primitives (also set inline by app.js where dynamic) */
.wall-outer  { stroke: var(--wall-outer);  fill: var(--wall-outer);  }
.wall-inner  { stroke: var(--wall-inner);  fill: var(--wall-inner);  }
.wall-railing{ stroke: var(--wall-railing); stroke-dasharray: 6 5; fill: none; }
.opening-gap { stroke: var(--opening); }
.window-pane { stroke: var(--window-pane); }
.door-arc    { stroke: var(--door-arc); fill: none; }
.room-poly   { fill: var(--room-fill); stroke: none; }
.room-name   { fill: var(--room-ink); font-weight: 600; }
.room-area   { fill: var(--ink-soft); }
.free-label  { fill: var(--label-ink); }

/* Furniture (editor-only — drawn above walls, below text labels) */
.furn-rect   { fill: var(--furn-fill); stroke: var(--furn-stroke); stroke-width: 1.5;
               vector-effect: non-scaling-stroke; }
.furn-tick   { stroke: var(--furn-tick); stroke-width: 2;
               vector-effect: non-scaling-stroke; stroke-linecap: round; }
.furn-abbr   { fill: var(--furn-ink); font-weight: 700; }
.furn-name   { fill: var(--furn-name); font-weight: 500; }

/* ---------- Overlay (loading / error) ---------- */
.overlay {
  position: absolute; inset: 0; z-index: 10;
  display: grid; place-items: center;
  background: rgba(244,245,247,.72); backdrop-filter: blur(2px);
}
.overlay.hidden { display: none; }
.overlay-card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 26px 30px; text-align: center; max-width: 380px;
}
.overlay-card p { margin: 14px 0 0; color: var(--ink-soft); }
.overlay-card.is-error p { color: var(--ink); }
.overlay-card .err-title { font-weight: 700; font-size: 16px; color: var(--danger); margin: 0; }
.spinner {
  width: 30px; height: 30px; margin: 0 auto; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
.overlay-card.is-error .spinner { display: none; }

@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Status bar ---------- */
.statusbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 16px; background: var(--panel);
  border-top: 1px solid var(--line);
  font-size: 12px; color: var(--ink-soft);
  flex: 0 0 auto;
}
.status-right { display: flex; align-items: center; gap: 8px; }
.dot { opacity: .5; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 54px; transform: translateX(-50%) translateY(10px);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 13px; box-shadow: var(--shadow); opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 50; max-width: 80vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.ok   { background: var(--ok); }
.toast.err  { background: var(--danger); }

@media (max-width: 720px) {
  .plan-title { max-width: 26vw; }
  .brand-name { display: none; }
  .context-panel { width: 200px; }
}

/* ===========================================================================
   Phase 2 — editing tools, context panel, selection overlay
   =========================================================================== */

/* Active tool highlight in the toolbar. */
.btn-icon.is-active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.btn-icon.is-active:hover { background: var(--accent-soft); }

/* Unsaved-changes indicator on the Save button. */
.dirty-dot {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); margin-right: 6px; vertical-align: middle;
}
.dirty-dot.hidden { display: none; }
.btn.is-dirty { border-color: var(--accent); }

/* Cursor hints by tool. */
.canvas-wrap.tool-crosshair { cursor: crosshair; }
.canvas-wrap.space-pan { cursor: grab; }
.canvas-wrap.editing-drag { cursor: grabbing; }

/* ---- Context panel (floating, top-right of the stage) ---- */
.context-panel {
  position: absolute; top: 14px; right: 14px; z-index: 8;
  width: 248px; max-height: calc(100% - 28px); overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 14px; font-size: 13px;
}
.context-panel.hidden { display: none; }

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  margin: -2px 0 10px; padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
}
.panel-title { font-weight: 700; font-size: 14px; }
.panel-del {
  width: 28px; height: 28px; font-size: 14px;
  border-color: transparent; background: transparent; color: var(--danger);
}
.panel-del:hover { background: #fdecea; }

.panel-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin: 8px 0;
}
.panel-row label { color: var(--ink-soft); flex: 0 0 auto; }
.panel-input {
  font: inherit; font-size: 13px; flex: 1 1 auto; min-width: 0;
  max-width: 132px;
  border: 1px solid var(--line-strong); border-radius: 6px;
  padding: 5px 8px; background: #fff; color: var(--ink);
}
.panel-input:focus { outline: none; border-color: var(--accent); }
select.panel-input { cursor: pointer; }
.panel-check { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }

/* ---- Selection / handle overlay (drawn inside the pan/zoom transform) ---- */
.editor-overlay { pointer-events: none; }

.ov-sel-wall  { stroke: var(--accent); stroke-width: 3; fill: none; opacity: .9; }
.ov-sel-room  { stroke: var(--accent); stroke-width: 2; fill: rgba(47,111,237,0.08);
                stroke-dasharray: 6 4; }
.ov-sel-ring  { stroke: var(--accent); stroke-width: 2; fill: none; }
.ov-handle    { fill: #fff; stroke: var(--accent); stroke-width: 2; }
.ov-snap      { fill: var(--ok); stroke: #fff; stroke-width: 2; }
.ov-preview   { stroke: var(--accent); stroke-width: 2.5; stroke-dasharray: 5 4; fill: none; }
.ov-ghost     { stroke: var(--accent); stroke-width: 6; opacity: .5; stroke-linecap: round; }
.ov-sel-furn  { stroke: var(--accent); stroke-width: 2.5; fill: rgba(47,111,237,0.08); }
/* Live dimension label (length in metres while drawing / on a selected wall). */
.ov-dim {
  fill: var(--accent); font-weight: 700;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  paint-order: stroke; stroke: #fff; stroke-width: 3.5px; stroke-linejoin: round;
  vector-effect: non-scaling-stroke; pointer-events: none; user-select: none;
}
/* Smart-alignment guide line (Figma-style), shown while dragging furniture. */
.ov-guide {
  stroke: #ff2d9b; stroke-width: 1; stroke-dasharray: 5 4; opacity: .9;
  vector-effect: non-scaling-stroke; pointer-events: none;
}

/* ---- Keyboard shortcuts overlay ---- */
.help-modal {
  position: absolute; inset: 0; z-index: 40;
  display: grid; place-items: center;
  background: rgba(15, 23, 42, 0.45); backdrop-filter: blur(2px);
}
.help-modal.hidden { display: none; }
.help-card {
  position: relative; background: var(--panel, #fff); color: var(--text, #0f172a);
  border-radius: 14px; padding: 22px 26px; min-width: 320px; max-width: 460px;
  box-shadow: 0 24px 60px rgba(0,0,0,.28); border: 1px solid var(--border, #e2e8f0);
}
.help-card h2 { margin: 0 0 14px; font-size: 17px; }
.help-card h3 {
  margin: 14px 0 6px; font-size: 11px; text-transform: uppercase;
  letter-spacing: .5px; color: var(--muted, #64748b);
}
.help-list { display: flex; flex-direction: column; gap: 4px; }
.help-row { display: flex; align-items: center; gap: 12px; font-size: 13px; }
.help-row kbd {
  flex: 0 0 auto; min-width: 46px; text-align: center;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px;
  padding: 3px 7px; border-radius: 6px; background: var(--bg, #f1f5f9);
  border: 1px solid var(--border, #e2e8f0); box-shadow: 0 1px 0 var(--border, #e2e8f0);
}
.help-close {
  position: absolute; top: 10px; right: 12px; border: none; background: none;
  font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted, #94a3b8);
}
.help-close:hover { color: var(--text, #0f172a); }
.lang-group .btn-seg { min-width: 30px; }

/* All-walls measurement labels (toggled by the measurements button). Font is
   world-sized so it scales with the plan; white halo keeps it readable. */
.dim-label {
  fill: #475569; font-weight: 600;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  paint-order: stroke; stroke: #ffffff; stroke-linejoin: round;
  pointer-events: none; user-select: none;
}

/* Instant custom tooltip for the (cryptic) toolbar icons. */
.ui-tip {
  position: fixed; z-index: 60; pointer-events: none;
  background: #0f172a; color: #fff; border-radius: 8px;
  padding: 7px 10px; max-width: 240px;
  box-shadow: 0 10px 26px rgba(0,0,0,.30);
  font-size: 12px; line-height: 1.35;
  display: flex; flex-direction: column; gap: 2px;
}
.ui-tip.hidden { display: none; }
.ui-tip-name { font-weight: 700; }
.ui-tip-desc { opacity: .82; }
.ov-furn-ghost{ stroke: var(--accent); stroke-width: 2; stroke-dasharray: 5 4;
                fill: rgba(47,111,237,0.06); opacity: .85; }

/* ---- Furniture category palette (popover under the toolbar button) ---- */
.furniture-palette {
  position: fixed; z-index: 30;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px;
  width: 300px; max-width: calc(100vw - 16px);
  padding: 10px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.furniture-palette.hidden { display: none; }

.furn-chip {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 8px 6px; font-size: 11px; font-weight: 600;
  border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--panel); color: var(--ink); cursor: pointer;
  text-align: center; line-height: 1.1;
}
.furn-chip:hover { background: #f0f2f6; }
.furn-chip.is-active {
  background: var(--accent-soft); border-color: var(--accent); color: var(--accent);
}
.furn-chip-abbr {
  display: grid; place-items: center; width: 28px; height: 28px;
  border-radius: 6px; background: var(--furn-fill); color: var(--furn-ink);
  font-size: 12px; font-weight: 800;
}
.furn-chip.is-active .furn-chip-abbr { background: #fff; color: var(--accent); }
.furn-chip-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

/* ---------- 2D / 3D view switch ---------- */
.btn-seg {
  min-width: 38px; height: 34px; padding: 0 10px;
  font-size: 12px; font-weight: 700;
  border-color: transparent; background: transparent; color: var(--ink-soft);
}
.btn-seg:hover { background: #eef1f6; }
.btn-seg.is-active {
  background: var(--accent-soft); color: var(--accent);
  border-color: var(--accent-soft);
}

/* ---------- 3D dollhouse view ---------- */
.three-wrap {
  position: absolute; inset: 0; z-index: 5;
  background:
    radial-gradient(120% 120% at 50% 0%, #fbfcfe 0%, #eef1f6 55%, #e4e8ef 100%);
  overflow: hidden;
}
.three-wrap.hidden { display: none; }
.three-canvas { width: 100%; height: 100%; display: block; cursor: grab; }
.three-canvas:active { cursor: grabbing; }
.three-controls {
  position: absolute; left: 0; right: 0; bottom: 16px; z-index: 6;
  display: flex; align-items: center; justify-content: center; gap: 14px;
  pointer-events: none;
}
.three-controls .btn { pointer-events: auto; }
.three-hint {
  font-size: 12px; color: var(--ink-soft);
  background: rgba(255,255,255,.78); padding: 5px 10px; border-radius: 999px;
  box-shadow: var(--shadow); backdrop-filter: blur(4px);
}
.three-badge {
  position: absolute; top: 14px; left: 50%; transform: translateX(-50%);
  z-index: 7; font-size: 12px; font-weight: 700; color: var(--ok);
  background: rgba(255,255,255,.92); padding: 6px 12px; border-radius: 999px;
  box-shadow: var(--shadow);
}
.three-badge.hidden { display: none; }

/* A small dot on Export PDF when a 3D angle is queued for inclusion. */
.btn-primary.has-3d { position: relative; }
.btn-primary.has-3d::after {
  content: ""; position: absolute; top: -3px; right: -3px;
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--ok); border: 2px solid var(--panel);
}
