/* =================================================================
   SVG Editor -- Ribbon-style UI with Sidebar
   Uses OS skin colors via --sz-color-* custom properties
   ================================================================= */

/* ---- Base ---- */
* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--sz-color-button-face, #ece9d8);
  color: var(--sz-color-button-text, #1e1e1e);
  font: 11px Tahoma, "Segoe UI", sans-serif;
  display: flex;
  flex-direction: column;
  user-select: none;
}

/* -- Quick Access Toolbar ---------------------------------------- */

.qat {
  display: flex;
  align-items: center;
  padding: 1px 4px;
  background: var(--sz-color-active-title);
  gap: 1px;
  flex-shrink: 0;
  min-height: 20px;
}

.qat-btn {
  width: 20px;
  height: 18px;
  border: none;
  background: transparent;
  cursor: default;
  font-size: 11px;
  line-height: 18px;
  text-align: center;
  color: var(--sz-color-title-text);
  border-radius: 2px;
  padding: 0;
}

.qat-btn:hover { background: rgba(255,255,255,0.25); }
.qat-btn:active { background: rgba(0,0,0,0.15); }

/* -- Ribbon Tab Bar ---------------------------------------------- */

.ribbon-tabs {
  flex-shrink: 0;
  background: var(--sz-color-button-face);
  border-bottom: 1px solid var(--sz-color-button-shadow);
}

.ribbon-tab-bar {
  display: flex;
  align-items: flex-end;
  padding: 0 2px;
}

.ribbon-file-btn {
  padding: 4px 14px;
  font-size: 11px;
  font-weight: bold;
  border: none;
  background: var(--sz-color-highlight);
  color: var(--sz-color-highlight-text);
  cursor: default;
  border-radius: 3px 3px 0 0;
  margin-right: 2px;
}

.ribbon-file-btn:hover { filter: brightness(1.1); }

.ribbon-tab {
  padding: 4px 12px;
  font-size: 11px;
  border: 1px solid transparent;
  border-bottom: none;
  background: transparent;
  color: var(--sz-color-button-text);
  cursor: default;
  border-radius: 3px 3px 0 0;
  position: relative;
  top: 1px;
}

.ribbon-tab:hover {
  background: var(--sz-color-button-highlight);
  border-color: var(--sz-color-button-shadow);
}

.ribbon-tab.active {
  background: var(--sz-color-window);
  border-color: var(--sz-color-button-shadow);
  color: var(--sz-color-window-text);
  font-weight: bold;
}

/* -- Backstage (File menu) --------------------------------------- */

.backstage {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 5000;
  background: rgba(0,0,0,0.3);
}

.backstage.visible { display: flex; }

.backstage-sidebar {
  width: 220px;
  background: var(--sz-color-highlight);
  display: flex;
  flex-direction: column;
  padding: 4px 0;
  box-shadow: 2px 0 8px rgba(0,0,0,0.3);
}

.backstage-back {
  border: none;
  background: transparent;
  color: var(--sz-color-highlight-text);
  font-size: 18px;
  padding: 8px 16px;
  text-align: left;
  cursor: default;
}

.backstage-back:hover { background: rgba(255,255,255,0.15); }

.backstage-item {
  border: none;
  background: transparent;
  color: var(--sz-color-highlight-text);
  font-size: 12px;
  padding: 8px 24px;
  text-align: left;
  cursor: default;
  width: 100%;
}

.backstage-item:hover { background: rgba(255,255,255,0.15); }
.backstage-sep { height: 1px; background: rgba(255,255,255,0.2); margin: 4px 12px; }
.backstage-spacer { flex: 1; }

/* -- Ribbon Panels ----------------------------------------------- */

.ribbon-panel {
  display: none;
  flex-direction: row;
  align-items: stretch;
  padding: 2px 4px 0;
  background: var(--sz-color-window);
  border-bottom: 1px solid var(--sz-color-button-shadow);
  flex-shrink: 0;
  min-height: 82px;
  overflow-x: auto;
  overflow-y: hidden;
}

.ribbon-panel.active { display: flex; }

.ribbon-panel::-webkit-scrollbar { height: 4px; }
.ribbon-panel::-webkit-scrollbar-thumb { background: var(--sz-color-button-shadow); border-radius: 2px; }

/* -- Ribbon Groups ----------------------------------------------- */

.ribbon-group {
  display: flex;
  flex-direction: column;
  padding: 2px 4px 0;
  min-width: 0;
  flex-shrink: 0;
}

.ribbon-group-content {
  display: flex;
  align-items: flex-start;
  gap: 2px;
  flex: 1;
  min-height: 0;
}

.ribbon-group-label {
  text-align: center;
  font-size: 9px;
  color: var(--sz-color-gray-text);
  padding: 1px 4px 2px;
  white-space: nowrap;
  border-top: 1px solid var(--sz-color-button-light, transparent);
}

.ribbon-sep {
  width: 1px;
  background: var(--sz-color-button-shadow);
  margin: 2px 3px;
  align-self: stretch;
  flex-shrink: 0;
  opacity: 0.4;
}

/* -- Ribbon Buttons ---------------------------------------------- */

.rb-btn {
  border: 1px solid transparent;
  background: transparent;
  cursor: default;
  color: var(--sz-color-button-text);
  font-size: 11px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 1px 3px;
  border-radius: 2px;
  white-space: nowrap;
}

.rb-btn:hover {
  background: var(--sz-color-button-highlight);
  border-color: var(--sz-color-button-shadow);
}

.rb-btn:active { background: var(--sz-color-button-shadow); }

.rb-btn.active {
  background: var(--sz-color-highlight);
  color: var(--sz-color-highlight-text);
  border-color: var(--sz-color-button-shadow);
}

.rb-btn-lg {
  flex-direction: column;
  padding: 4px 8px;
  min-width: 44px;
  height: 100%;
  gap: 2px;
}

.rb-btn-lg .rb-icon-lg { font-size: 20px; line-height: 1; }
.rb-btn-lg .rb-label { font-size: 9px; }

.rb-btn-sm {
  gap: 4px;
  padding: 1px 4px;
  min-height: 20px;
  justify-content: flex-start;
}

.rb-btn-sm .rb-icon { font-size: 12px; width: 16px; text-align: center; flex-shrink: 0; }
.rb-btn-sm .rb-label { font-size: 10px; }

.rb-stack {
  display: flex;
  flex-direction: column;
  gap: 1px;
  justify-content: flex-start;
}

.rb-row {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-wrap: nowrap;
}

/* -- Tool Grid (4x2 in Home ribbon) ------------------------------ */

.tool-grid-4x2 {
  display: grid;
  grid-template-columns: repeat(4, 22px);
  gap: 1px;
}

.tool-grid-4x2 .rb-btn {
  width: 22px;
  height: 22px;
  padding: 0;
  font-size: 11px;
}

/* -- Ribbon inline inputs ---------------------------------------- */

.rb-inline-label {
  font-size: 10px;
  white-space: nowrap;
  min-width: 38px;
  display: inline;
  margin: 0;
}

.rb-color-input {
  width: 48px;
  height: 18px;
  padding: 0;
  border: 1px solid var(--sz-color-button-shadow, #888);
  cursor: default;
}

.rb-num-input {
  width: 52px;
  height: 18px;
  padding: 1px 2px;
  font-size: 10px;
  border: 1px solid var(--sz-color-button-shadow, #888);
  background: var(--sz-color-window, #fff);
  color: var(--sz-color-window-text, #000);
}

/* ---- Main area ---- */
.main-area {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0;
}

/* ---- Workspace ---- */
.workspace {
  flex: 1;
  overflow: auto;
  background:
    linear-gradient(45deg, #d0d0d0 25%, transparent 25%) 0 0/24px 24px,
    linear-gradient(-45deg, #d0d0d0 25%, transparent 25%) 0 0/24px 24px,
    linear-gradient(45deg, transparent 75%, #d0d0d0 75%) 0 0/24px 24px,
    linear-gradient(-45deg, transparent 75%, #d0d0d0 75%) 0 0/24px 24px,
    #e8e8e8;
  min-width: 0;
}

body.dark .workspace {
  background:
    linear-gradient(45deg, #333 25%, transparent 25%) 0 0/24px 24px,
    linear-gradient(-45deg, #333 25%, transparent 25%) 0 0/24px 24px,
    linear-gradient(45deg, transparent 75%, #333 75%) 0 0/24px 24px,
    linear-gradient(-45deg, transparent 75%, #333 75%) 0 0/24px 24px,
    #2a2a2a;
}

.svg-host {
  padding: 20px;
  min-width: fit-content;
  min-height: fit-content;
  transform-origin: 0 0;
  position: relative;
}

#svgCanvas {
  background: #fff;
  border: 1px solid #444;
  display: block;
}

body.dark #svgCanvas {
  border-color: #666;
}

/* ---- Sidebar ---- */
.sidebar {
  width: 200px;
  min-width: 160px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--sz-color-button-face, #ece9d8);
  border-left: 1px solid var(--sz-color-button-shadow, #888);
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 0 4px;
  scrollbar-gutter: stable;
}

.panel {
  border-bottom: 1px solid var(--sz-color-button-shadow, #888);
  padding: 0 6px 6px;
  margin: 0;
}

.panel-toggle {
  cursor: pointer;
  user-select: none;
  list-style: none;
  padding: 4px 0;
}

.panel-toggle::-webkit-details-marker {
  display: none;
}

.panel-toggle h2 {
  display: inline;
  font-size: 10px;
  text-transform: uppercase;
  margin: 0;
  font-weight: bold;
  color: var(--sz-color-button-text, #1e1e1e);
}

.panel-toggle::before {
  content: '\25bc';
  display: inline-block;
  width: 12px;
  font-size: 8px;
  transition: transform 0.15s;
}

details:not([open]) > .panel-toggle::before {
  transform: rotate(-90deg);
}

.row {
  display: flex;
  gap: 4px;
  margin-top: 4px;
  flex-wrap: wrap;
}

.sidebar label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 3px;
  gap: 6px;
  font-size: 11px;
}

input[type="number"],
input[type="color"],
textarea {
  border: 1px solid var(--sz-color-button-shadow, #888);
  background: var(--sz-color-window, #fff);
  color: var(--sz-color-window-text, #000);
  font: inherit;
}

.sidebar input[type="number"] { width: 70px; }
.sidebar input[type="color"] { width: 70px; height: 18px; padding: 0; }

button {
  border: 1px solid var(--sz-color-button-shadow, #888);
  padding: 2px 8px;
  background: var(--sz-color-button-face, #ece9d8);
  color: var(--sz-color-button-text, #1e1e1e);
  cursor: default;
  font: inherit;
}

button:hover {
  background: var(--sz-color-button-highlight, #fff);
}

button:active {
  border-style: inset;
}

/* ---- Layer list ---- */
.layer-list {
  max-height: 120px;
  overflow: auto;
  border: 1px solid var(--sz-color-button-shadow, #888);
  background: var(--sz-color-window, #fff);
  padding: 1px;
  margin-top: 4px;
}

.layer-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 4px;
  align-items: center;
  padding: 2px 4px;
  cursor: default;
  font-size: 10px;
  user-select: none;
}

.layer-item:hover {
  background: var(--sz-color-button-highlight, #f0f0f0);
}

.layer-item.active {
  background: var(--sz-color-highlight, #2a62db);
  color: var(--sz-color-highlight-text, #fff);
}

/* ---- Source panel ---- */
.panel textarea {
  width: 100%;
  height: 100px;
  resize: vertical;
  box-sizing: border-box;
  font: 10px Consolas, "Courier New", monospace;
  margin-top: 4px;
}

/* ---- Status bar ---- */
.status-bar {
  display: flex;
  align-items: center;
  padding: 2px 8px;
  background: var(--sz-color-button-face, #ece9d8);
  border-top: 1px solid var(--sz-color-button-shadow, #888);
  font-size: 10px;
  color: var(--sz-color-button-text, #1e1e1e);
  flex-shrink: 0;
  user-select: none;
  gap: 12px;
  min-height: 22px;
}

.status-section {
  white-space: nowrap;
}

.status-spacer { flex: 1; }

/* ---- Selection dash on selected SVG element ---- */
.sel {
  stroke: #2a62db !important;
  stroke-dasharray: 4 3;
}

/* ---- Resize handles ---- */
.handle {
  position: absolute;
  width: 8px;
  height: 8px;
  background: #fff;
  border: 1px solid #2a62db;
  z-index: 50;
  box-sizing: border-box;
}

.handle[data-pos="nw"] { cursor: nw-resize; }
.handle[data-pos="n"]  { cursor: n-resize; }
.handle[data-pos="ne"] { cursor: ne-resize; }
.handle[data-pos="e"]  { cursor: e-resize; }
.handle[data-pos="se"] { cursor: se-resize; }
.handle[data-pos="s"]  { cursor: s-resize; }
.handle[data-pos="sw"] { cursor: sw-resize; }
.handle[data-pos="w"]  { cursor: w-resize; }

/* ---- Dialogs ---- */
.dialog-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.3);
  z-index: 4000;
  justify-content: center;
  align-items: center;
}

.dialog-overlay.visible {
  display: flex;
}

.dialog {
  background: var(--sz-color-button-face, #ece9d8);
  border: 2px outset var(--sz-color-button-highlight, #fff);
  min-width: 280px;
  max-width: 400px;
  box-shadow: 3px 3px 8px rgba(0,0,0,0.3);
  border-radius: 4px;
}

.dialog.dialog-wide {
  min-width: 360px;
  max-width: 480px;
}

.dialog-title {
  background: var(--sz-color-highlight, #2a62db);
  color: var(--sz-color-highlight-text, #fff);
  padding: 4px 8px;
  font-weight: bold;
  font-size: 11px;
  user-select: none;
  border-radius: 4px 4px 0 0;
}

.dialog-body {
  padding: 12px;
  font-size: 11px;
  color: var(--sz-color-button-text, #1e1e1e);
}

.dialog-body p {
  margin: 0 0 6px;
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  padding: 8px 12px;
  border-top: 1px solid var(--sz-color-button-shadow);
}

.dialog-buttons button {
  min-width: 72px;
  padding: 3px 12px;
  font-size: 11px;
  border: 1px solid var(--sz-color-button-shadow);
  background: var(--sz-color-button-face);
  color: var(--sz-color-button-text);
  border-radius: 3px;
}

.dialog-buttons button:hover { background: var(--sz-color-button-highlight); }

.dialog-buttons button:first-child {
  background: var(--sz-color-highlight);
  color: var(--sz-color-highlight-text);
  border-color: var(--sz-color-highlight);
}

.dialog-buttons button:first-child:hover { filter: brightness(1.1); }

/* ---- Shortcut table ---- */
.shortcut-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}

.shortcut-table th {
  text-align: left;
  border-bottom: 1px solid var(--sz-color-button-shadow, #888);
  padding: 2px 8px;
  font-weight: bold;
}

.shortcut-table td {
  padding: 2px 8px;
  border-bottom: 1px solid var(--sz-color-button-highlight, #eee);
}

.shortcut-table tr:hover td {
  background: var(--sz-color-button-highlight, #f0f0f0);
}

/* ---- Dark mode ---- */
body.dark {
  --dark-bg: #2b2b2b;
  --dark-text: #ccc;
}

body.dark #svgCanvas {
  background: #1e1e1e;
}

body.dark .handle {
  background: #333;
  border-color: #7aa3ff;
}
