:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1d2632;
  --muted: #5b6675;
  --line: #d9e1ea;
  --grid-major: #cfd9e6;
  --grid-minor: #e7edf5;
  --plot: #18324d;
  --setback: rgba(255, 194, 122, 0.28);
  --corridor: #d6f0ff;
  --room-stroke: #203247;
  --selected: #ff8a00;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  color: var(--ink);
  background: radial-gradient(circle at top left, #f9fbff, var(--bg) 42%);
  font-family: "Avenir Next", "Segoe UI", sans-serif;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

.app-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.app-header h1 {
  margin: 0;
  font-size: 1.95rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #10263b;
}
.app-header p { margin: 4px 0 0; color: var(--muted); font-size: 0.9rem; }

.layout {
  display: grid;
  grid-template-columns: 360px 1fr;
  min-height: calc(100vh - 76px);
  min-width: 0;
}

.controls {
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 12px;
  overflow: auto;
  min-width: 0;
}
.controls section { margin-bottom: 14px; }
.control-panel {
  border: 1px solid #d8e2ee;
  border-radius: 12px;
  padding: 10px;
  background: #fcfdff;
}
.mobile-panel-nav {
  display: none;
}
.controls h2 {
  margin: 6px 0 10px;
  font-size: 1rem;
}
.controls h3 {
  margin: 10px 0 8px;
  font-size: 0.9rem;
  color: #2b4057;
}
label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.83rem;
  color: #33485e;
}
input, select, button, textarea {
  border: 1px solid #c8d3df;
  border-radius: 8px;
  padding: 8px;
  font: inherit;
}
button {
  background: #18324d;
  color: #fff;
  cursor: pointer;
}
button:hover { background: #24486f; }
.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.btn-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.tabs button {
  background: #eff4fa;
  color: #1a2c41;
  border: 1px solid #ccd8e4;
}
.tabs button.active {
  background: #18324d;
  color: #fff;
}
.room-list {
  display: grid;
  gap: 6px;
  max-height: 240px;
  overflow: auto;
  margin-top: 8px;
}
.room-item {
  border: 1px solid #ccd8e4;
  border-radius: 8px;
  padding: 6px 8px;
  background: #f8fbff;
  cursor: pointer;
  text-align: left;
}
.room-item.active {
  border-color: #18324d;
  background: #e7f1fb;
}
.room-item-title {
  font-size: 0.84rem;
  font-weight: 600;
}
.room-item-meta {
  font-size: 0.76rem;
  color: #42576b;
}
.save-status {
  margin-top: 6px;
  font-size: 0.75rem;
  color: #445a70;
}

#warnings {
  margin: 0;
  padding-left: 18px;
  color: #7b2f0f;
  font-size: 0.86rem;
}

.canvas-area {
  padding: 12px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.canvas-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}
.canvas-tools {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.canvas-tools button {
  min-height: 36px;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.82rem;
  white-space: nowrap;
}
.legend {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  min-width: 0;
}
.chip {
  border: 1px solid #ccd8e4;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 0.78rem;
  background: #f8fbff;
}
.mobile-canvas-btn {
  display: none;
}

#canvasWrap {
  position: relative;
  width: min(86vw, calc(100vh - 170px));
  height: min(86vw, calc(100vh - 170px));
  max-width: 900px;
  max-height: 900px;
  min-width: 520px;
  min-height: 520px;
  border: 1px solid #b8c7d8;
  background: #fff;
}

#plannerCanvas {
  --cell: 8px;
  --major: 40px;
  width: 100%;
  height: 100%;
  position: relative;
  background-image:
    linear-gradient(var(--grid-major) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-major) 1px, transparent 1px),
    linear-gradient(var(--grid-minor) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-minor) 1px, transparent 1px);
  background-size: var(--major) var(--major), var(--major) var(--major), var(--cell) var(--cell), var(--cell) var(--cell);
  background-position: 0 0, 0 0, 0 0, 0 0;
}

#northArrow {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 8;
  border: 1px solid #9cb0c6;
  background: #fff;
  border-radius: 8px;
  padding: 4px 8px;
  font-weight: 700;
}

.plot-boundary {
  position: absolute;
  border: 2px solid var(--plot);
  pointer-events: none;
  z-index: 1;
}

.setback-zone {
  position: absolute;
  border: 1px dashed #cd8f3c;
  background: var(--setback);
  pointer-events: none;
  z-index: 1;
}

.room {
  position: absolute;
  border: 1px solid var(--room-stroke);
  z-index: 3;
  overflow: visible;
  cursor: move;
  user-select: none;
  touch-action: none;
}
.room.selected {
  outline: 2px solid var(--selected);
  z-index: 6;
}
.room-label {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 4px;
  line-height: 1.2;
  background: rgba(255, 255, 255, 0.82);
}
.room-dim {
  font-size: 9px;
  padding: 1px 4px;
  background: rgba(255, 255, 255, 0.82);
}
.room-center {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 6;
  text-align: center;
  pointer-events: none;
  display: grid;
  gap: 2px;
  max-width: calc(100% - 8px);
}
.room-center-overlay {
  position: absolute;
  transform: translate(-50%, -50%);
  z-index: 21;
  text-align: center;
  pointer-events: none;
  display: grid;
  gap: 2px;
  max-width: 160px;
}

.handle {
  position: absolute;
  z-index: 9;
  touch-action: none;
  width: 10px;
  height: 10px;
  background: #ffffff;
  border: 1px solid #17324b;
  border-radius: 2px;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.65);
}

.handle.side {
  transform: translate(-50%, -50%);
}
.handle.side.n,
.handle.side.s { cursor: ns-resize; }
.handle.side.e,
.handle.side.w { cursor: ew-resize; }

.handle.corner.ne { top: 1px; right: 1px; cursor: nesw-resize; }
.handle.corner.nw { top: 1px; left: 1px; cursor: nwse-resize; }
.handle.corner.se { bottom: 1px; right: 1px; cursor: nwse-resize; }
.handle.corner.sw { bottom: 1px; left: 1px; cursor: nesw-resize; }

.handle.inner {
  background: #ffd88f;
  border-color: #7f5200;
  transform: translate(-50%, -50%);
}
.handle.inner.iv { cursor: ew-resize; }
.handle.inner.ih { cursor: ns-resize; }

.opening {
  position: absolute;
  z-index: 7;
  border-radius: 999px;
  cursor: grab;
  touch-action: none;
}
.opening.door {
  height: 6px;
  background: #18324d;
  border: 1px solid #0f2235;
}
.opening.window {
  height: 6px;
  background: #88cef6;
  border: 1px solid #2d8dc8;
}
.opening.dragging {
  cursor: grabbing;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.7);
}

.dim-line {
  position: absolute;
  z-index: 20;
  font-size: 10px;
  color: #24415d;
  background: rgba(255,255,255,0.86);
  border: 1px solid #c3d4e7;
  border-radius: 4px;
  padding: 1px 4px;
  pointer-events: none;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

#inspector {
  font-size: 0.86rem;
  color: #2f4256;
  line-height: 1.45;
}
.opening-controls {
  margin-top: 8px;
}
.opening-tools {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.opening-tools button {
  background: #eef4fa;
  color: #1a3046;
  border: 1px solid #bfccda;
  padding: 6px 8px;
}
.opening-tools button:hover {
  background: #dceaf7;
}
.opening-tools button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.opening-hint {
  margin-top: 6px;
  font-size: 0.76rem;
  color: #486178;
}
.shape-tools {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-top: 8px;
}
.shape-tools button {
  background: #eef4fa;
  color: #1a3046;
  border: 1px solid #bfccda;
  padding: 6px 8px;
}
.shape-tools button:hover {
  background: #dceaf7;
}
.shape-tools button.active {
  background: #18324d;
  color: #fff;
  border-color: #18324d;
}
.shape-tools button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

dialog {
  width: min(900px, 95vw);
  border: 1px solid #c8d3df;
  border-radius: 10px;
}
#jsonOutput {
  width: 100%;
  height: 52vh;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

@media (max-width: 1080px) {
  .layout { 
    display: flex;
    flex-direction: column;
    min-height: auto;
  }
  .canvas-area { order: 1; }
  .mobile-canvas-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    border-radius: 999px;
    padding: 8px 12px;
    background: #18324d;
    color: #fff;
    border: 1px solid #18324d;
    font-size: 0.84rem;
    white-space: nowrap;
  }
  .layout.canvas-focus .controls {
    display: none;
  }
  .layout.canvas-focus .canvas-area {
    order: 1;
    padding: 6px;
  }
  .layout.canvas-focus .canvas-header {
    position: sticky;
    top: 0;
    z-index: 30;
    background: #f6f9fd;
    border: 1px solid #d4deea;
    border-radius: 10px;
    padding: 6px;
    margin-bottom: 6px;
  }
  .layout.canvas-focus #canvasWrap {
    width: min(calc(100vw - 12px), calc(100vh - 90px));
    height: min(calc(100vw - 12px), calc(100vh - 90px));
  }
  .controls {
    order: 2;
    border-right: none;
    border-top: 1px solid var(--line);
    border-bottom: none;
    padding: 10px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }
  .controls section { margin-bottom: 10px; }
  .mobile-panel-nav {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    position: sticky;
    top: 0;
    z-index: 30;
    background: #f7faff;
    border: 1px solid #d5dfeb;
    border-radius: 10px;
    padding: 6px;
    margin-bottom: 10px;
    -webkit-overflow-scrolling: touch;
  }
  .mobile-panel-nav button {
    flex: 0 0 auto;
    min-height: 40px;
    border-radius: 999px;
    padding: 8px 12px;
    background: #eef4fa;
    color: #15324c;
    border: 1px solid #ccd8e4;
    font-size: 0.86rem;
  }
  .mobile-panel-nav button.active {
    background: #18324d;
    color: #fff;
    border-color: #18324d;
  }
  .controls.mobile-panels .control-panel {
    display: none;
  }
  .controls.mobile-panels .control-panel.active {
    display: block;
  }
  .canvas-header {
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
  }
  .canvas-tools {
    width: 100%;
  }
  .canvas-tools button {
    min-height: 40px;
    flex: 1 1 140px;
  }
  .legend {
    width: 100%;
    overflow-x: auto;
    flex-wrap: nowrap;
    padding-bottom: 2px;
    -webkit-overflow-scrolling: touch;
  }
  .chip { flex: 0 0 auto; }
  #canvasWrap {
    min-width: 0;
    min-height: 0;
    width: min(calc(100vw - 16px), 96vh);
    height: min(calc(100vw - 16px), 96vh);
  }
  .btn-row {
    flex-wrap: wrap;
  }
  .btn-row button {
    flex: 1 1 120px;
    min-height: 42px;
  }
  input, select, button, textarea {
    min-height: 40px;
    font-size: 16px;
  }
  .handle {
    width: 14px;
    height: 14px;
  }
  .opening {
    height: 8px;
  }
  .shape-tools button,
  .opening-tools button {
    min-height: 42px;
  }
}

@media (max-width: 640px) {
  .app-header {
    padding: 10px 14px;
  }
  .app-header h1 {
    font-size: 1.4rem;
    letter-spacing: 0.03em;
    line-height: 1.15;
  }
  .app-header p {
    font-size: 0.8rem;
  }
  .canvas-area {
    padding: 8px;
  }
  .control-panel {
    border-radius: 10px;
    padding: 8px;
  }
  .mobile-panel-nav {
    gap: 6px;
    padding: 5px;
    margin-bottom: 8px;
  }
  .mobile-panel-nav button {
    min-height: 36px;
    padding: 6px 10px;
    font-size: 0.8rem;
  }
  .mobile-canvas-btn {
    min-height: 36px;
    padding: 6px 10px;
    font-size: 0.78rem;
  }
  .controls h2 { font-size: 0.95rem; margin: 4px 0 8px; }
  .controls h3 { font-size: 0.84rem; margin: 8px 0 6px; }
  .room-list { max-height: 180px; }
  #northArrow {
    top: 6px;
    right: 6px;
    padding: 3px 7px;
  }
  .grid2 {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 420px) {
  .app-header {
    padding: 9px 10px;
  }
  .app-header h1 {
    font-size: 1.2rem;
    letter-spacing: 0.02em;
  }
  .app-header p {
    font-size: 0.74rem;
  }
  .controls {
    padding: 8px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  }
  .mobile-panel-nav button {
    min-height: 34px;
    padding: 5px 9px;
    font-size: 0.76rem;
  }
  .mobile-canvas-btn {
    min-height: 34px;
    padding: 5px 9px;
    font-size: 0.74rem;
  }
  .btn-row button {
    flex: 1 1 100%;
  }
  #canvasWrap {
    width: calc(100vw - 10px);
    height: calc(100vw - 10px);
  }
}

@media (max-width: 360px) {
  .app-header h1 {
    font-size: 1.05rem;
  }
  .controls {
    padding: 6px;
  }
  .control-panel {
    padding: 7px;
  }
  .mobile-panel-nav {
    gap: 5px;
    padding: 4px;
  }
  .mobile-panel-nav button {
    font-size: 0.72rem;
    padding: 4px 8px;
    min-height: 32px;
  }
  .mobile-canvas-btn {
    min-height: 32px;
    padding: 4px 8px;
    font-size: 0.7rem;
  }
  .room-item-title { font-size: 0.78rem; }
  .room-item-meta { font-size: 0.7rem; }
  #northArrow {
    font-size: 0.76rem;
    padding: 2px 6px;
  }
}
