:root {
  --navy: #10253c;
  --ink: #141c29;
  --blue: #2e5aac;
  --blue-dark: #254b92;
  --cyan: #00a3b8;
  --green: #1f8255;
  --amber: #b5701a;
  --red: #c5392f;
  --bg: #eef1f5;
  --panel: #ffffff;
  --line: #d3dee7;
  --text: #1a2937;
  --muted: #3d4a58;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 "Yu Gothic UI", Meiryo, system-ui, sans-serif;
}

button,
select,
input,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: #f7fafc;
  color: var(--navy);
  min-height: 34px;
  padding: 7px 10px;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

button:focus-visible,
select:focus-visible,
input:focus-visible,
textarea:focus-visible,
canvas:focus-visible {
  outline: 3px solid #8be3ed;
  outline-offset: 2px;
}

.app-shell {
  height: 100vh;
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  padding-bottom: 88px;
}

.app-shell.command-log-1 {
  padding-bottom: 70px;
}

.app-shell.command-log-3 {
  padding-bottom: 106px;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 10px 18px;
  color: #fff;
  background: linear-gradient(125deg, var(--ink), #1a5076 65%, #007e91);
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.eyebrow {
  margin: 0;
  color: #91e3ec;
  font-size: 11px;
  font-weight: 800;
}

h1 {
  margin: 1px 0 0;
  font-size: 20px;
  line-height: 1.1;
}

h2 {
  margin: 0 0 10px;
  color: var(--navy);
  font-size: 15px;
  border-bottom: 2px solid #dceef2;
  padding-bottom: 6px;
}

.quick-access {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  border-radius: 6px;
  background: #ffffff0f;
}

.quick-btn {
  width: 30px;
  min-height: 30px;
  display: grid;
  place-items: center;
  border: none;
  background: transparent;
  color: #b7c7d6;
  border-radius: 4px;
  padding: 0;
}

.quick-btn:hover {
  background: #ffffff22;
  color: #fff;
}

.quick-btn svg {
  pointer-events: none;
}

.drawing-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.topbar-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
}

.topbar-command {
  min-width: 72px;
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-color: #ffffff66;
  background: #ffffff12;
  color: #fff;
  border-radius: 4px;
}

.topbar-command:hover {
  background: #ffffff22;
}

.drawing-meta span,
.drawing-meta strong,
.drawing-meta label {
  border: 1px solid #ffffff55;
  border-radius: 4px;
  padding: 4px 8px;
}

.drawing-meta select {
  margin-left: 4px;
}

.ribbon-tabs {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  background: #dee4eb;
  padding: 5px 10px 0;
  overflow-x: auto;
}

.ribbon-tab {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 4px 4px 0 0;
  padding: 6px 14px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  min-height: 30px;
}

.ribbon-tab.active {
  background: var(--panel);
  color: var(--text);
  border-color: var(--line);
}

.ribbon {
  display: flex;
  gap: 2px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 4px 6px 0;
  overflow-x: auto;
  min-height: 84px;
}

.ribbon-group {
  display: flex;
  flex-direction: column;
  padding: 0 6px;
  border-right: 1px solid #e9edf2;
  flex-shrink: 0;
}

.ribbon-group-buttons {
  display: flex;
  gap: 3px;
  flex: 1;
  align-items: flex-start;
}

.ribbon-group-label {
  text-align: center;
  font-size: 10px;
  color: var(--muted);
  padding: 2px 4px 4px;
}

.ribbon-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  min-height: 30px;
  background: #fff;
  border: 1px solid transparent;
  border-radius: 3px;
  color: #2b3a4e;
  font-size: 11px;
  line-height: 1.15;
  white-space: nowrap;
}

.ribbon-btn:hover {
  background: #edf2f8;
  border-color: #c9d7ec;
}

.ribbon-btn.active {
  background: #e9f0fb;
  border-color: var(--blue);
}

.ribbon-btn.big {
  flex-direction: column;
  justify-content: center;
  min-width: 54px;
  min-height: 62px;
  padding: 5px 3px 3px;
  text-align: center;
}

.ribbon-btn svg {
  flex-shrink: 0;
  pointer-events: none;
}

.workspace {
  --dock-width: 300px;

  display: grid;
  grid-template-columns: minmax(420px, 1fr) var(--dock-width);
  grid-template-rows: minmax(0, 1fr);
  gap: 10px;
  min-height: 0;
  overflow: hidden;
  padding: 10px;
  position: relative;
}

.dock section,
.canvas-panel,
.command-line {
  border: 1px solid var(--line);
  background: var(--panel);
}

.dock-resize-handle {
  position: absolute;
  z-index: 6;
  top: 10px;
  bottom: 10px;
  right: calc(2px + var(--dock-width));
  width: 16px;
  display: grid;
  place-items: center;
  color: #718596;
  cursor: col-resize;
  touch-action: none;
}

.dock-resize-handle::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: 8px;
  width: 2px;
  background: #b8c8d4;
}

.dock-resize-handle span {
  position: relative;
  padding: 5px 0;
  background: var(--bg);
  font-weight: 800;
}

.dock-resize-handle:hover::before,
.dock-resize-handle.active::before,
.dock-resize-handle:focus-visible::before {
  background: var(--cyan);
}

.canvas-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
}

.space-tabs {
  display: flex;
  align-items: center;
  gap: 2px;
  min-width: 0;
  overflow-x: auto;
  padding: 3px 8px 0;
  background: #e7ebf0;
  border-bottom: 1px solid var(--line);
}

.space-tab {
  background: #dde3ea;
  color: var(--muted);
  border: 1px solid var(--line);
  border-bottom: none;
  border-radius: 3px 3px 0 0;
  padding: 3px 14px;
  font-size: 11px;
  font-weight: 700;
  min-height: 26px;
}

.space-tab.active {
  background: var(--panel);
  color: var(--text);
}

.space-tabs .spacer {
  flex: 1;
}

.space-tabs .zoom-readout {
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
}

.canvas-toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  min-width: 0;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
}

#cadCanvas {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: block;
  cursor: crosshair;
}

.layout-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  min-width: 0;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 11.5px;
}

.layout-toolbar label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-weight: 700;
}

.layout-toolbar select,
.layout-toolbar input {
  width: auto;
  padding: 3px 6px;
}

.layout-toolbar .spacer {
  flex-basis: 100%;
  height: 0;
}

.layout-stage {
  min-height: 0;
  overflow: auto;
  display: grid;
  place-items: center;
  padding: 24px;
  background: #c7cfd9;
}

.layout-page {
  background: #fff;
  box-shadow: 0 8px 30px rgba(16, 36, 64, 0.25);
  position: relative;
  flex-shrink: 0;
}

.layout-margin {
  position: absolute;
  border: 1px dashed #b6c0cc;
}

.layout-viewport {
  position: absolute;
  border: 1px solid var(--blue);
  background: #f5f8fc;
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.layout-titleblock {
  position: absolute;
  border: 1.5px solid #334155;
  background: #fff;
  font-size: 10px;
  color: var(--text);
}

.layout-titleblock .row {
  display: grid;
  border-bottom: 1px solid #334155;
}

.layout-titleblock .row:last-child {
  border-bottom: none;
}

.layout-titleblock .cell {
  padding: 3px 6px;
  border-right: 1px solid #334155;
}

.layout-titleblock .cell:last-child {
  border-right: none;
}

.layout-titleblock .cell.label {
  background: #f2f4f8;
  font-weight: 700;
}

.inspector,
.dock {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.dock-tabs {
  display: flex;
  border-bottom: 1px solid #e3e8ef;
  background: var(--bg);
  flex-shrink: 0;
}

.dock-tab {
  flex: 1;
  padding: 7px 2px;
  font-size: 11px;
  font-weight: 700;
  border: none;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--muted);
}

.dock-tab.active {
  border-bottom-color: var(--blue);
  background: #f8fafb;
  color: var(--text);
}

.dock-body {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px;
}

.dock-body section {
  padding: 0;
  border: none;
}

.compact-form,
.layer-create-form {
  display: grid;
  gap: 8px;
}

.compact-form label {
  display: grid;
  gap: 3px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.compact-form dl {
  grid-template-columns: 54px 1fr;
  padding: 7px;
  background: #f8fbfd;
  border: 1px solid var(--line);
}

.inline-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 7px;
}

.layer-create-form {
  grid-template-columns: minmax(0, 1fr) 34px 34px;
  margin-bottom: 8px;
}

.layer-create-form input[type="color"] {
  min-height: 34px;
  padding: 2px;
}

.layer-create-form button {
  padding: 3px;
}

.measure-result,
.empty-note {
  margin: 8px 0 0;
  padding: 7px;
  border: 1px solid var(--line);
  background: #f8fbfd;
  color: var(--muted);
  font-size: 12px;
}

textarea,
input,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 7px;
  background: #fff;
  color: var(--text);
}

.button-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 7px;
  margin-top: 8px;
}

.wide {
  width: 100%;
  margin-top: 8px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
}

.segmented button {
  min-height: 30px;
  padding: 4px 5px;
  border-radius: 4px;
  font-size: 12px;
}

.segmented button.active {
  border-color: var(--cyan);
  background: #e6f8fb;
  color: #006d7c;
  font-weight: 800;
}

.api-status {
  display: grid;
  grid-template-columns: 38px 1fr;
  gap: 6px;
  align-items: center;
  margin-top: 9px;
  padding: 8px;
  border: 1px solid var(--line);
  background: #f8fbfd;
}

.api-status span {
  color: var(--muted);
  font-weight: 800;
}

.api-status.ok {
  border-color: #bfe5d7;
  background: #eefaf5;
}

.api-status.loading {
  border-color: #c9d9e6;
  background: #f2f7fb;
}

.api-status.error {
  border-color: #efc6c6;
  background: #fff3f3;
}

.button-row button:last-child:nth-child(3) {
  grid-column: 1 / -1;
}

.preview-box {
  margin-top: 9px;
  padding: 10px;
  background: #f8fbfd;
  border: 1px solid var(--line);
  border-radius: 4px;
}

.layer-list {
  display: grid;
  gap: 6px;
}

.layer-row {
  display: grid;
  grid-template-columns: 18px 16px 1fr 54px;
  gap: 6px;
  align-items: center;
}

.swatch {
  width: 14px;
  height: 14px;
  min-height: 14px;
  padding: 0;
  border: 1px solid #ffffff;
  box-shadow: 0 0 0 1px var(--line);
  opacity: 1;
}

.swatch::-webkit-color-swatch-wrapper {
  padding: 0;
}

.swatch::-webkit-color-swatch {
  border: 0;
}

.mini {
  min-height: 24px;
  padding: 2px 5px;
  font-size: 11px;
}

.mini.locked {
  border-color: var(--amber);
  background: #fff5df;
}

dl {
  display: grid;
  grid-template-columns: 90px 1fr;
  gap: 4px 8px;
  margin: 0;
}

dt {
  color: var(--muted);
  font-weight: 700;
}

dd {
  margin: 0;
}

.issue-list {
  margin: 0;
  padding-left: 18px;
  max-height: 130px;
  overflow: auto;
}

.issue-list li {
  margin-bottom: 4px;
}

.critical {
  color: var(--red);
}

.major {
  color: var(--amber);
}

.minor {
  color: var(--muted);
}

.ok {
  color: var(--green);
  font-weight: 800;
}

.warn {
  color: var(--amber);
  font-weight: 700;
}

.command-line {
  min-height: 88px;
  max-height: 88px;
  overflow: hidden;
  padding: 7px 10px;
  background: #0d2136;
  color: #dbeaf5;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
  display: grid;
  grid-template-rows: minmax(0, 1fr) 34px;
  gap: 6px;
  position: fixed;
  z-index: 20;
  right: 0;
  bottom: 0;
  left: 0;
}

.command-line.command-log-1 {
  min-height: 70px;
  max-height: 70px;
}

.command-line.command-log-3 {
  min-height: 106px;
  max-height: 106px;
}

.command-history {
  overflow: auto;
  line-height: 18px;
}

.command-form {
  display: grid;
  grid-template-columns: 16px minmax(0, 1fr) 38px;
  gap: 6px;
  align-items: center;
}

.command-form input {
  min-width: 0;
  height: 32px;
  border-color: #4d657b;
  background: #132c45;
  color: #ffffff;
  font-family: inherit;
  padding: 5px 8px;
}

.command-form button {
  min-height: 32px;
  padding: 3px;
  border-color: #4d657b;
  background: #1c405e;
  color: #ffffff;
}

.status-bar {
  height: 26px;
  flex-shrink: 0;
  min-width: 0;
  overflow-x: auto;
  background: #e7ebf0;
  border-top: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 10px;
  font-size: 11px;
  color: var(--muted);
}

.status-bar .coord {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  min-width: 150px;
}

.status-bar .divider {
  width: 1px;
  height: 14px;
  background: var(--line);
}

.status-toggles {
  display: flex;
  gap: 3px;
}

.status-toggle {
  border-radius: 3px;
  padding: 2px 9px;
  font-size: 10.5px;
  font-weight: 700;
  min-height: 22px;
  background: #fff;
  border: 1px solid var(--line);
  color: var(--muted);
}

.status-toggle[aria-pressed="true"] {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}

.status-bar .spacer {
  flex: 1;
}

dialog {
  width: min(440px, calc(100vw - 28px));
  max-height: calc(100vh - 40px);
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0;
  color: var(--text);
  box-shadow: 0 18px 55px #10253c40;
}

dialog::backdrop {
  background: #10253c99;
}

dialog form {
  display: grid;
  gap: 14px;
  padding: 18px;
}

dialog header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

dialog header h2 {
  flex: 1;
  margin: 0;
}

dialog label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-weight: 700;
}

.dialog-close {
  min-width: 34px;
  padding: 3px;
  border: 0;
  background: transparent;
  font-size: 20px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.settings-group {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
}

.settings-group legend {
  padding: 0 5px;
  color: var(--navy);
  font-weight: 800;
}

.toggle-row {
  grid-template-columns: 20px 1fr;
  align-items: center;
}

.toggle-row input {
  width: 18px;
  height: 18px;
  margin: 0;
}

.system-summary {
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: #f8fbfd;
}

.settings-actions span {
  flex: 1;
}

.primary {
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
}

@media (max-width: 980px) {
  .app-shell {
    height: auto;
    min-height: 100vh;
  }

  .workspace {
    grid-template-columns: 1fr;
    overflow: visible;
  }

  .dock {
    grid-column: 1 / -1;
  }

  .dock-resize-handle {
    display: none;
  }
}

@media (max-width: 680px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar-actions {
    width: 100%;
    align-items: flex-end;
  }

  .drawing-meta {
    flex: 1;
  }

  .ribbon-group-label {
    display: none;
  }

  #cadCanvas {
    height: min(40vh, 360px);
  }
}

@media print {
  @page {
    size: A3 landscape;
    margin: 10mm;
  }

  body {
    background: #fff;
  }

  .topbar,
  .ribbon-tabs,
  .ribbon,
  .space-tabs,
  .dock,
  .dock-resize-handle,
  .canvas-toolbar,
  .layout-toolbar,
  .status-bar,
  .command-line {
    display: none !important;
  }

  .app-shell,
  .workspace,
  .canvas-panel {
    display: block;
    height: auto;
    padding: 0;
    border: 0;
  }

  #cadCanvas,
  .layout-stage {
    width: 100%;
    height: auto;
    max-height: none;
  }
}
