:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1d1f24;
  --muted: #6b7080;
  --border: #dcdfe5;
  --hover: #eceef2;
  --active: #dbe6fb;
  --accent: #2667d8;
  --workspace: #e3e6eb;
  --ruler-bg: #f8f9fb;
  --ruler-fg: #5b6070;
  --shadow: 0 8px 28px rgba(0, 0, 0, .16);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #1f2024;
  --panel: #26282d;
  --text: #e8e9ec;
  --muted: #9ba0ad;
  --border: #383b42;
  --hover: #33363d;
  --active: #2c3f63;
  --accent: #6c9cf0;
  --workspace: #17181b;
  --ruler-bg: #222428;
  --ruler-fg: #a4a8b4;
  --shadow: 0 8px 28px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column;
  background: var(--bg); color: var(--text);
  font: 13px/1.35 system-ui, -apple-system, "Segoe UI", Ubuntu, Cantarell, sans-serif;
  overflow: hidden; user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; }
input, select { font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 3px 6px; }
input[type="number"] { width: 70px; }
kbd { font: inherit; color: var(--muted); font-size: 11px; }
[hidden] { display: none !important; }

/* ------------------------------------------------ menu bar */
#menubar {
  display: flex; align-items: center; gap: 6px; padding: 4px 8px;
  background: var(--panel); border-bottom: 1px solid var(--border); flex: none;
}
.menus { display: flex; gap: 2px; }
.menu { position: relative; }
.menu-btn { background: none; border: 0; padding: 5px 10px; border-radius: 6px; cursor: pointer; }
.menu-btn:hover, .menu.open .menu-btn { background: var(--hover); }
.menu-pop {
  display: none; position: absolute; top: calc(100% + 4px); left: 0; z-index: 50; min-width: 240px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 5px; box-shadow: var(--shadow);
}
.menu.open .menu-pop { display: block; }
.menu-pop button, .popover button {
  display: flex; width: 100%; justify-content: space-between; align-items: center; gap: 18px;
  background: none; border: 0; padding: 6px 10px; border-radius: 6px; text-align: left; cursor: pointer;
}
.menu-pop button:hover, .popover button:hover { background: var(--hover); }
.menu-pop button:disabled { opacity: .4; cursor: default; background: none; }
.menu-pop hr, .popover hr { border: 0; border-top: 1px solid var(--border); margin: 4px 2px; }
.menu-pop button[data-toggle] span::before, .popover button.radio span::before { content: ""; display: inline-block; width: 18px; }
.menu-pop button[data-toggle].on span::before, .popover button.radio.on span::before { content: "✓"; }
.quick { display: flex; gap: 2px; margin-left: 6px; }
#title { flex: 1; text-align: center; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; min-width: 30px; height: 30px; padding: 0 4px;
  background: none; border: 1px solid transparent; border-radius: 6px; cursor: pointer;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn.on, .icon-btn[aria-pressed="true"] { background: var(--active); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.icon-btn:disabled { opacity: .35; cursor: default; background: none; }

/* ------------------------------------------------ toolbar */
#toolbar {
  display: flex; align-items: stretch; gap: 0; padding: 6px 8px 2px; overflow-x: auto; overflow-y: hidden;
  background: var(--panel); border-bottom: 1px solid var(--border); flex: none; scrollbar-width: thin;
}
.group { display: flex; flex-direction: column; align-items: center; padding: 0 10px; border-right: 1px solid var(--border); flex: none; }
.group:last-child { border-right: 0; }
.group-body { flex: 1; display: flex; align-items: center; gap: 4px; }
.group-caption { font-size: 11px; color: var(--muted); padding: 3px 0 1px; }
.grid3 { display: grid; grid-template-columns: repeat(3, auto); gap: 2px; }
.grid2b { display: grid; grid-template-columns: repeat(2, auto); gap: 2px; }
.split { display: flex; align-items: center; }
.split .icon-btn:first-child { height: 52px; min-width: 44px; }
.split .drop { min-width: 18px; padding: 0; height: 52px; }
.big-btn { display: flex; flex-direction: column; align-items: center; gap: 2px; height: auto; padding: 4px; font-size: 11px; }
.shape-grid { display: grid; grid-template-columns: repeat(8, 26px); gap: 1px; }
.shape-grid .icon-btn { min-width: 26px; width: 26px; height: 24px; padding: 0; }
.stack { display: flex; flex-direction: column; gap: 4px; }
.stack select { width: 132px; }
.row { display: flex; align-items: center; gap: 6px; }
#opacity { width: 100px; }

.slots { display: flex; gap: 6px; }
.slot { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--muted); background: none; border: 1px solid transparent; border-radius: 6px; padding: 3px; cursor: pointer; }
.slot.on { background: var(--active); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.slot .sw { width: 30px; height: 30px; }
.palette { display: grid; grid-template-columns: repeat(10, 18px); gap: 3px; }
.sw { width: 18px; height: 18px; border-radius: 4px; border: 1px solid rgba(0,0,0,.25); cursor: pointer; padding: 0; }
.sw.empty { border: 1px dashed var(--border); background: transparent; }
:root[data-theme="dark"] .sw { border-color: rgba(255,255,255,.25); }

.popover {
  position: fixed; z-index: 60; background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 5px; box-shadow: var(--shadow); min-width: 190px;
}

/* ------------------------------------------------ text options */
#textbar {
  display: flex; align-items: center; gap: 8px; padding: 5px 10px; flex: none;
  background: var(--panel); border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
#font-family { width: 200px; }
.check { display: inline-flex; align-items: center; gap: 6px; }
.check input { margin: 0; }

/* ------------------------------------------------ work area */
#main { flex: 1; display: flex; min-height: 0; }
#work { flex: 1; display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto 1fr; min-width: 0; min-height: 0; }
.ruler { display: block; background: var(--ruler-bg); }
#ruler-corner { width: 20px; height: 20px; border-right: 1px solid var(--border); border-bottom: 1px solid var(--border); }
#work.no-rulers { grid-template-columns: 0 1fr; grid-template-rows: 0 1fr; }
#work.no-rulers .ruler { display: none; }
#canvas-area { position: relative; min-width: 0; min-height: 0; grid-column: 2; grid-row: 2; background: var(--workspace); }
#scroller { position: absolute; inset: 0; overflow: auto; touch-action: none; }
#spacer { pointer-events: auto; }
#display { position: absolute; left: 0; top: 0; pointer-events: none; }
#text-input {
  position: absolute; left: 0; top: 0; width: 1px; height: 1px; opacity: 0; resize: none; border: 0; padding: 0;
  pointer-events: none; z-index: -1;
}

#layers-panel {
  width: 240px; flex: none; display: flex; flex-direction: column;
  background: var(--panel); border-left: 1px solid var(--border);
}
#layers-panel h2 { font-size: 13px; margin: 10px 12px 6px; }
#layer-list { list-style: none; margin: 0; padding: 0 6px; flex: 1; overflow: auto; }
#layer-list li { display: flex; align-items: center; gap: 8px; padding: 5px 6px; border-radius: 8px; cursor: pointer; }
#layer-list li:hover { background: var(--hover); }
#layer-list li.active { background: var(--active); }
#layer-list canvas { width: 52px; height: 38px; border-radius: 3px; border: 1px solid var(--border); background: #ddd; flex: none; }
#layer-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#layer-list input.name { padding: 1px 4px; }
.layer-buttons { display: flex; justify-content: space-between; padding: 6px; border-top: 1px solid var(--border); }

/* ------------------------------------------------ status bar */
#statusbar {
  display: flex; align-items: center; gap: 16px; padding: 2px 10px; flex: none;
  background: var(--panel); border-top: 1px solid var(--border); color: var(--muted); font-size: 12px; min-height: 30px;
}
.stat { display: inline-flex; align-items: center; gap: 5px; min-width: 110px; }
.stat i { display: inline-flex; }
.spacer { flex: 1; }
#zoom-slider { width: 140px; padding: 0; border: 0; background: none; accent-color: var(--accent); }
#zoom-label { min-width: 44px; text-align: right; }

/* ------------------------------------------------ dialogs */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 18px 20px; min-width: 300px; max-width: 92vw;
  background: var(--panel); color: var(--text); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog h2 { margin: 0 0 12px; font-size: 16px; }
dialog form > label, dialog fieldset { display: flex; flex-direction: column; gap: 4px; margin: 0 0 10px; }
dialog fieldset { border: 0; padding: 0; flex-direction: row; flex-wrap: wrap; gap: 14px; }
dialog fieldset legend { font-weight: 600; margin-bottom: 6px; width: 100%; }
dialog fieldset .grid2 { width: 100%; }
dialog .check { flex-direction: row; margin-bottom: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.grid2 label { display: flex; flex-direction: column; gap: 4px; }
.grid2 input { width: 100%; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; margin: 14px 0 0; padding: 0; }
dialog menu button { border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 6px 14px; cursor: pointer; }
dialog menu button:hover { background: var(--hover); }
dialog menu button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
dl#prop-info { display: grid; grid-template-columns: auto 1fr; gap: 3px 14px; margin: 0 0 12px; }
dl#prop-info dt { color: var(--muted); }
dl#prop-info dd { margin: 0; user-select: text; }
.hint { color: var(--muted); font-size: 12px; }

#toast {
  position: fixed; left: 50%; bottom: 46px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: #1d1f24; color: #fff; padding: 8px 14px; border-radius: 10px; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 100; max-width: 80vw; text-align: center;
}
#toast.show { opacity: .95; transform: translateX(-50%); }

/* ------------------------------------------------ print */
#print-area { display: none; }
@media print {
  body > *:not(#print-area) { display: none !important; }
  body { overflow: visible; background: #fff; }
  #print-area { display: block; }
  #print-img { max-width: 100%; max-height: 100vh; }
}

/* ------------------------------------------------ small screens */
@media (max-width: 720px) {
  .quick, #title { display: none; }
  #layers-panel { position: absolute; right: 0; top: 0; bottom: 0; z-index: 20; box-shadow: var(--shadow); }
  #main { position: relative; }
}
