:root {
  color-scheme: light;
  --bg: #f3f5f7;
  --panel: #ffffff;
  --line: #d6d9de;
  --text: #111827;
  --subtle: #556070;
  --brand: #1d4ed8;
  --danger: #b42318;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.app-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex-shrink: 0;
}

.app-header h1 {
  margin: 0;
  font-size: 1.5rem;
}

.brand-block {
  display: grid;
  gap: 4px;
}

.app-header p {
  margin: 4px 0 0;
  color: var(--subtle);
}

.storage-status {
  font-size: 0.9rem;
}

.fixture-hint {
  margin: 0;
  font-size: 0.85rem;
  color: var(--subtle);
}

.plan-controls {
  display: flex;
  gap: 8px;
  align-items: center;
}

.export-dropdown {
  position: relative;
}

.import-dropdown {
  position: relative;
}

.export-dropdown-summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  display: inline-block;
  margin: 0;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  font: inherit;
}

.export-dropdown-summary::-webkit-details-marker {
  display: none;
}

.export-dropdown[open] .export-dropdown-summary {
  border-color: var(--brand);
  color: var(--brand);
}

.import-dropdown-summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  display: inline-block;
  margin: 0;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  font: inherit;
}

.import-dropdown-summary::-webkit-details-marker {
  display: none;
}

.import-dropdown[open] .import-dropdown-summary {
  border-color: var(--brand);
  color: var(--brand);
}

.export-dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 220px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
  z-index: 40;
}

.import-dropdown-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: min(360px, calc(100vw - 32px));
  max-height: min(80vh, 620px);
  overflow: auto;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
  z-index: 40;
}

.export-dropdown-item {
  width: 100%;
  text-align: left;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.9rem;
}

.export-dropdown-item:hover {
  background: #eff4ff;
  border-color: #bfd0ff;
}

input,
button {
  font: inherit;
}

input[type="text"],
input[type="number"],
select {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 180px;
  background: white;
}

button {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  background: var(--brand);
  color: white;
}

button.ghost {
  background: #fff;
  color: var(--text);
  border-color: var(--line);
}

button.danger {
  background: var(--danger);
}

button.danger-ghost {
  background: white;
  color: var(--danger);
  border-color: #f0b4b0;
}

.app-toolstrip {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex-shrink: 0;
}

.toolstrip-row {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 10px 12px;
  width: 100%;
}

.toolstrip-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
}

/* Keep tool buttons evenly sized across the row. */
.toolstrip-tools > .tool-btn,
.toolstrip-tools > .door-tool-dropdown,
.toolstrip-tools > .window-tool-dropdown,
.toolstrip-tools > .fixture-tool-dropdown {
  flex: 1 1 0;
  min-width: 0;
}

.toolstrip-tools > details > summary.tool-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.toolstrip-end {
  flex: 0 0 auto;
  margin-left: auto;
  display: flex;
  align-items: flex-end;
}

.open-plan-block {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin: 0;
  align-items: flex-end;
}

.open-plan-title {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--subtle);
  line-height: 1;
}

.open-plan-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
}

.open-plan-select {
  min-width: 10rem;
  max-width: min(18rem, 42vw);
  width: auto;
}

.open-plan-delete {
  padding: 6px 10px;
  font-size: 0.85rem;
  white-space: nowrap;
}

.toolstrip-sep {
  width: 1px;
  height: 28px;
  background: var(--line);
  flex-shrink: 0;
}

.toolstrip-tools .toolstrip-sep {
  display: none;
}

.app-toolstrip .tool-btn {
  padding: 6px 10px;
  font-size: 0.9rem;
  white-space: nowrap;
}

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
  padding: 12px;
  overflow: hidden;
}

.layout.layout--with-side-panel {
  grid-template-columns: minmax(0, 1fr) 180px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  min-height: 0;
}

.side-panel {
  overflow: auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.side-panel.panel-hidden {
  display: none;
}

.side-panel h2 {
  margin-top: 0;
}

.side-panel .room-form input[type="text"],
.side-panel .room-form input[type="number"],
.side-panel .room-form input[type="color"],
.side-panel .room-form input[type="file"],
.side-panel .room-form input[type="range"],
.side-panel .room-form select {
  min-width: 0;
  width: 100%;
  max-width: 100%;
}

.properties-stack {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.side-panel > .properties-stack:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}

.properties-stack .room-form {
  border-top: none;
  padding-top: 0;
}

.tool-btn {
  flex: 1;
  background: #eff4ff;
  border-color: #bfd0ff;
  color: #2044a3;
}

.tool-btn.active {
  background: #2044a3;
  color: white;
}

.app-toolstrip .tool-btn:hover {
  background: #dbe7ff;
  border-color: #9fb8ff;
  color: #173b9b;
}

.app-toolstrip .tool-btn.active:hover {
  background: #2044a3;
  border-color: #2044a3;
  color: white;
}

.door-tool-dropdown {
  position: relative;
  flex: 0 0 auto;
}

.door-tool-summary.tool-btn {
  flex: 0 0 auto;
  list-style: none;
  cursor: pointer;
  user-select: none;
  border: 1px solid #bfd0ff;
  border-radius: 8px;
}

.door-tool-summary.tool-btn::-webkit-details-marker {
  display: none;
}

.door-tool-dropdown[open] .door-tool-summary.tool-btn:not(.active) {
  border-color: var(--brand);
  color: var(--brand);
}

.door-tool-menu {
  --door-menu-max-h: min(56vh, calc(100vh - 10rem), 340px);
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: auto;
  z-index: 38;
  width: min(300px, calc(100vw - 32px));
  max-height: var(--door-menu-max-h);
  overflow: auto;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.door-tool-intro {
  margin: 0;
  font-size: 0.82rem;
  color: var(--subtle);
  line-height: 1.35;
}

.door-tool-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.door-tool-item {
  text-align: left;
  padding: 7px 9px;
  font-size: 0.88rem;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

.door-tool-item:hover {
  background: #eff4ff;
  border-color: #bfd0ff;
}

.fixture-tool-dropdown {
  position: static;
  flex: 0 0 auto;
}

.fixture-tool-summary.tool-btn {
  flex: 0 0 auto;
  list-style: none;
  cursor: pointer;
  user-select: none;
  border: 1px solid #bfd0ff;
  border-radius: 8px;
}

.fixture-tool-summary.tool-btn::-webkit-details-marker {
  display: none;
}

.fixture-tool-dropdown[open] .fixture-tool-summary.tool-btn:not(.active) {
  border-color: var(--brand);
  color: var(--brand);
}

.fixture-tool-menu {
  --fixture-menu-max-h: min(68vh, calc(100vh - 11rem), 560px);
  position: absolute;
  /* Anchor to full toolstrip row so a wide panel stays inside the page (no right overhang). */
  top: calc(100% + 6px);
  left: auto;
  right: max(10px, env(safe-area-inset-right, 0px));
  z-index: 38;
  width: min(1120px, calc(100% - 16px));
  max-width: calc(100vw - 32px);
  max-height: var(--fixture-menu-max-h);
  padding: 0;
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
}

/* Inner pane: flex+scroll on the outer panel often prevents overflow; this layer scrolls reliably. */
.fixture-tool-menu-body {
  box-sizing: border-box;
  max-height: var(--fixture-menu-max-h);
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  scrollbar-width: thin;
  scrollbar-color: #94a3b8 #f1f5f9;
  padding: 12px 16px;
  display: block;
}

.fixture-tool-menu-body > .fixture-tool-section + .fixture-tool-section {
  margin-top: 14px;
}

.fixture-tool-menu-body::-webkit-scrollbar {
  width: 10px;
}

.fixture-tool-menu-body::-webkit-scrollbar-track {
  background: #f1f5f9;
  border-radius: 6px;
  margin: 4px 0;
}

.fixture-tool-menu-body::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 6px;
  border: 2px solid #f1f5f9;
}

.fixture-tool-menu-body::-webkit-scrollbar-thumb:hover {
  background: #94a3b8;
}

.fixture-tool-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.fixture-tool-heading {
  margin: 0;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--subtle);
}

.fixture-tool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(8.25rem, 1fr));
  gap: 8px;
}

.fixture-tool-item {
  text-align: left;
  padding: 7px 9px;
  font-size: 0.88rem;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

.fixture-tool-item:hover {
  background: #eff4ff;
  border-color: #bfd0ff;
}

.fixture-tool-item--with-thumb {
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
}

.fixture-tool-item-label {
  flex: 1;
  min-width: 0;
}

.fixture-tool-thumb {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  object-fit: contain;
  border-radius: 6px;
  background: #f8fafc;
  border: 1px solid var(--line);
}

.fixture-tool-grid--with-thumbs {
  grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
}

.window-tool-dropdown {
  position: relative;
  flex: 0 0 auto;
}

.window-tool-summary.tool-btn {
  flex: 0 0 auto;
  list-style: none;
  cursor: pointer;
  user-select: none;
  border: 1px solid #bfd0ff;
  border-radius: 8px;
}

.window-tool-summary.tool-btn::-webkit-details-marker {
  display: none;
}

.window-tool-dropdown[open] .window-tool-summary.tool-btn:not(.active) {
  border-color: var(--brand);
  color: var(--brand);
}

.window-tool-menu {
  --window-menu-max-h: min(62vh, calc(100vh - 10rem), 420px);
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: auto;
  z-index: 38;
  width: min(360px, calc(100vw - 32px));
  max-height: var(--window-menu-max-h);
  overflow: auto;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.window-tool-intro {
  margin: 0;
  font-size: 0.82rem;
  color: var(--subtle);
  line-height: 1.35;
}

.window-tool-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.window-tool-item {
  text-align: left;
  padding: 7px 9px;
  font-size: 0.88rem;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

.window-tool-item:hover {
  background: #eff4ff;
  border-color: #bfd0ff;
}

.room-form {
  display: grid;
  gap: 10px;
}

.room-form.hidden {
  display: none;
}

.room-form label.hidden {
  display: none;
}

.room-form label {
  display: grid;
  gap: 4px;
  color: var(--subtle);
}

.opening-actions {
  display: flex;
  gap: 8px;
}

.opening-actions.hidden {
  display: none;
}

.canvas-wrap {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.canvas-toolbar p {
  margin: 4px 0;
  color: var(--subtle);
}

.canvas-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.zoom-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.compass-toolbar-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.compass-show {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--subtle);
  font-size: 0.85rem;
  cursor: pointer;
  user-select: none;
}

.compass-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--subtle);
  margin-left: 4px;
  font-size: 0.85rem;
}

#compassRotationInput {
  width: 120px;
  min-width: 120px;
}

#compassRotationValue {
  min-width: 38px;
  text-align: right;
  font-weight: 600;
  color: var(--text);
}

#zoomLevelLabel {
  min-width: 52px;
  text-align: center;
  color: var(--subtle);
  font-weight: 600;
}

.canvas-scroll {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  flex: 1;
  min-height: 0;
  background: #ffffff;
}

#plannerCanvas {
  display: block;
  border: 0;
  cursor: crosshair;
}

.underlay-hint,
.underlay-status {
  margin: 0;
  font-size: 0.85rem;
  color: var(--subtle);
}

.underlay-status {
  min-height: 2.5em;
}

.underlay-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.underlay-form input[type="range"] {
  width: 100%;
}

@media (max-width: 1180px) {
  body {
    overflow: auto;
    height: auto;
    min-height: 100vh;
  }

  .layout {
    grid-template-columns: 1fr;
    flex: none;
    min-height: 0;
    overflow: visible;
  }

  .side-panel {
    overflow: visible;
  }

  .toolstrip-end {
    margin-left: 0;
    width: 100%;
    justify-content: flex-end;
  }

  .open-plan-block {
    align-items: stretch;
    width: 100%;
    max-width: 24rem;
    margin-left: auto;
  }

  .open-plan-controls {
    justify-content: flex-start;
  }

  .open-plan-select {
    flex: 1 1 auto;
    max-width: none;
  }

  .fixture-tool-menu {
    --fixture-menu-max-h: min(62vh, calc(100vh - 10rem), 520px);
    right: max(10px, env(safe-area-inset-right, 0px));
    width: min(1120px, calc(100% - 12px));
    max-width: calc(100vw - 20px);
  }

  #plannerCanvas {
    max-height: none;
  }
}
