:root {
  --bg: #f4f1ea;
  --surface: #fffdf9;
  --surface-soft: #f8f5ef;
  --ink: #272b29;
  --muted: #898b84;
  --line: #dedbd2;
  --orange: #ef7746;
  --orange-soft: #fff0e8;
  --green: #52755d;
  --shadow: 0 14px 40px rgba(50, 47, 38, 0.08);
  --font: "Noto Sans SC", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  color: var(--ink);
  font-family: var(--font);
  background:
    radial-gradient(circle at 8% 4%, rgba(235, 173, 117, 0.12), transparent 26rem),
    linear-gradient(135deg, rgba(255,255,255,.35), transparent 45%),
    var(--bg);
  overflow: hidden;
}

button, input { font: inherit; }
button { color: inherit; }
button:focus-visible, input:focus-visible { outline: 3px solid rgba(239, 119, 70, .25); outline-offset: 2px; }

.app-shell { height: 100vh; min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
  height: 76px;
  flex: 0 0 76px;
  padding: 0 28px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 24px;
  background: rgba(255, 253, 249, 0.88);
  border-bottom: 1px solid rgba(199, 195, 185, .72);
  backdrop-filter: blur(16px);
  position: relative;
  z-index: 10;
}

.brand { color: inherit; text-decoration: none; display: flex; align-items: center; width: max-content; }

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: var(--ink);
  display: grid;
  grid-template-columns: repeat(2, 7px);
  grid-template-rows: repeat(2, 7px);
  gap: 4px;
  place-content: center;
  margin-right: 10px;
  transform: rotate(-3deg);
  box-shadow: 0 5px 12px rgba(39, 43, 41, .18);
}

.brand-mark i { background: #f6e1a6; border-radius: 2px; }
.brand-mark i:nth-child(2), .brand-mark i:nth-child(3) { background: var(--orange); }
.brand-name { font-weight: 800; font-size: 21px; letter-spacing: .08em; }
.brand-tagline { margin-left: 12px; padding-left: 12px; border-left: 1px solid var(--line); color: var(--muted); font-size: 12px; }

.project-title-wrap { text-align: center; display: flex; flex-direction: column; align-items: center; }
.project-title {
  width: 230px;
  padding: 2px 8px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 7px;
  text-align: center;
  font-weight: 700;
  font-size: 15px;
  color: var(--ink);
}
.project-title:hover, .project-title:focus { background: #fff; border-color: var(--line); }
.save-state { display: flex; gap: 5px; align-items: center; color: #9a9b95; font-size: 10px; margin-top: 3px; }
.save-dot { width: 5px; height: 5px; border-radius: 99px; background: #74a178; }

.top-actions { display: flex; justify-content: flex-end; gap: 10px; }
.project-file-btn { padding-inline: 12px; }
.btn { border: 0; border-radius: 10px; height: 40px; padding: 0 16px; display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-weight: 600; font-size: 13px; cursor: pointer; transition: .18s ease; }
.btn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.btn:hover { transform: translateY(-1px); }
.btn-ghost { background: #fffdfa; border: 1px solid var(--line); }
.btn-ghost:hover { border-color: #bbb7ad; background: #fff; }
.btn-dark { color: white; background: var(--ink); box-shadow: 0 6px 16px rgba(39,43,41,.16); }
.btn-dark:hover { background: #171a18; box-shadow: 0 8px 20px rgba(39,43,41,.22); }

.workspace {
  min-height: 0;
  flex: 1;
  display: grid;
  grid-template-columns: 210px minmax(460px, 1fr) 258px;
  gap: 16px;
  padding: 16px;
  max-width: 1700px;
  width: 100%;
  margin: auto;
}

.panel {
  background: rgba(255, 253, 249, .84);
  border: 1px solid rgba(210, 206, 196, .82);
  border-radius: 16px;
  box-shadow: 0 8px 28px rgba(67, 61, 48, .035);
  min-height: 0;
}

.tool-panel { padding: 22px 14px 14px; display: flex; flex-direction: column; }
.panel-label { padding: 0 8px; color: #9a9992; font-weight: 700; font-size: 10px; letter-spacing: .13em; text-transform: uppercase; }
.tool-list { display: grid; gap: 4px; margin-top: 9px; }
.tool {
  width: 100%; height: 42px; border: 0; border-radius: 9px; background: transparent; cursor: pointer;
  display: grid; grid-template-columns: 25px 1fr auto; align-items: center; padding: 0 10px; text-align: left; color: #6d6e68; font-size: 13px; transition: .15s ease;
}
.tool svg, .icon-action svg, .toggle-row svg, .clear-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tool:hover { background: #f6f2eb; color: var(--ink); }
.tool.active { background: var(--orange-soft); color: #cf5f34; font-weight: 700; }
.tool kbd { border: 0; font-family: "DM Mono", monospace; font-size: 9px; color: #aaa9a2; background: rgba(255,255,255,.7); padding: 2px 5px; border-radius: 4px; }
.tool.active kbd { color: #d37855; }
.panel-divider { height: 1px; background: var(--line); opacity: .7; margin: 19px 8px; }

.compact-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 10px; }
.icon-action { border: 1px solid var(--line); border-radius: 9px; background: #fffdfa; height: 58px; display: flex; flex-direction: column; gap: 4px; align-items: center; justify-content: center; font-size: 10px; color: #74756f; cursor: pointer; }
.icon-action:hover:not(:disabled) { color: var(--ink); border-color: #c6c2b8; background: #fff; }
.icon-action:disabled { opacity: .36; cursor: default; }

.toggle-row { margin: 6px 4px 0; height: 35px; display: flex; align-items: center; justify-content: space-between; font-size: 11px; cursor: pointer; }
.toggle-row > span { display: flex; align-items: center; gap: 8px; color: #63645e; }
.toggle-row svg { width: 16px; }
.toggle-row input { position: absolute; opacity: 0; }
.toggle-row i { width: 30px; height: 17px; border-radius: 20px; background: #d7d5ce; position: relative; transition: .2s; }
.toggle-row i::after { content: ""; position: absolute; width: 13px; height: 13px; top: 2px; left: 2px; border-radius: 50%; background: white; box-shadow: 0 1px 3px rgba(0,0,0,.2); transition: .2s; }
.toggle-row input:checked + i { background: var(--orange); }
.toggle-row input:checked + i::after { transform: translateX(13px); }

.panel-tip { margin-top: auto; border-radius: 11px; background: #f4f0e8; color: #88877f; font-size: 10px; line-height: 1.7; padding: 12px; }
.panel-tip span { display: block; color: #5f625d; font-weight: 700; margin-bottom: 2px; }

.canvas-section { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.canvas-toolbar, .canvas-footer { height: 42px; flex: 0 0 42px; display: flex; align-items: center; padding: 0 4px; }
.canvas-meta { display: flex; align-items: center; gap: 9px; font-size: 10px; color: #9b9a93; }
.canvas-meta strong { color: #5e605c; font-family: "DM Mono", monospace; font-size: 11px; }
.resize-button {
  height: 26px; padding: 0 8px; display: inline-flex; align-items: center; gap: 5px;
  border: 1px solid rgba(199, 195, 185, .82); border-radius: 7px; background: rgba(255,255,255,.62);
  color: #76776f; font-size: 10px; cursor: pointer;
}
.resize-button:hover { color: var(--ink); border-color: #bbb7ad; background: #fff; }
.resize-button svg { width: 13px; height: 13px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.status-pill { padding: 4px 7px; background: rgba(255,255,255,.65); border: 1px solid rgba(213,209,199,.8); border-radius: 6px; display: flex; align-items: center; gap: 5px; }
.status-pill i { width: 5px; height: 5px; border-radius: 99px; background: var(--orange); }
.zoom-control { margin-left: auto; display: flex; align-items: center; gap: 6px; color: #8d8d87; }
.zoom-control button { width: 24px; height: 24px; display: grid; place-items: center; border: 1px solid transparent; background: transparent; border-radius: 6px; cursor: pointer; font-size: 16px; color: #7c7d77; }
.zoom-control button:hover { background: #fff; border-color: var(--line); }
.zoom-control input { width: 80px; height: 3px; accent-color: var(--ink); cursor: pointer; }
.zoom-control output { font: 10px "DM Mono", monospace; width: 35px; text-align: right; }

.canvas-viewport {
  min-height: 0;
  flex: 1;
  border-radius: 16px;
  border: 1px solid #d8d4ca;
  background-color: #e9e5dc;
  background-image: radial-gradient(rgba(106, 105, 98, .12) .8px, transparent .8px);
  background-size: 14px 14px;
  position: relative;
  overflow: auto;
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 48px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.6);
}

.board-stage { position: relative; flex: 0 0 auto; margin: auto; }
#beadCanvas {
  width: 540px;
  height: 540px;
  display: block;
  position: relative;
  z-index: 2;
  cursor: crosshair;
  background: #faf9f5;
  border: 1px solid rgba(140, 137, 128, .35);
  border-radius: 5px;
  box-shadow: 0 14px 32px rgba(62,57,47,.18), 0 2px 6px rgba(62,57,47,.12);
  touch-action: none;
}
.board-shadow { position: absolute; z-index: 1; inset: 14px -13px -14px 13px; background: rgba(98, 92, 80, .12); filter: blur(14px); border-radius: 12px; }
.canvas-viewport.pan-ready #beadCanvas { cursor: grab !important; }
.canvas-viewport.is-panning, .canvas-viewport.is-panning #beadCanvas { cursor: grabbing !important; user-select: none; }
.ruler { position: absolute; font: 8px "DM Mono", monospace; color: #99978e; z-index: 3; pointer-events: none; }
.ruler-top { left: 0; right: 0; top: -22px; display: flex; }
.ruler-left { top: 0; bottom: 0; left: -27px; display: flex; flex-direction: column; }
.ruler span { flex: 1; display: flex; align-items: center; justify-content: center; }

.canvas-footer { color: #8c8c85; font-size: 10px; }
.canvas-footer b { color: #545651; font-weight: 700; }
.footer-divider { height: 11px; width: 1px; background: #ccc9c0; margin: 0 10px; }
.footer-spacer { flex: 1; }
.footer-help { opacity: .8; }
.filing-links { display: flex; align-items: center; gap: 12px; margin-left: 14px; }
.filing-link { color: #8c8c85; text-decoration: none; transition: color .15s ease; white-space: nowrap; }
.filing-link:hover { color: #555752; text-decoration: underline; text-underline-offset: 2px; }
.police-filing-link { display: inline-flex; align-items: center; gap: 4px; }
.police-filing-link svg { width: 12px; height: 12px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.color-panel { padding: 22px 16px 14px; display: flex; flex-direction: column; overflow: hidden; }
.color-heading { display: flex; align-items: center; justify-content: space-between; padding-bottom: 14px; }
.color-heading strong { display: block; font-size: 13px; margin: 5px 0 0 8px; }
.color-heading small { display: block; margin: 2px 0 0 8px; color: #9b9a93; font: 9px "DM Mono", monospace; }
.active-color-chip { width: 50px; height: 34px; padding: 3px; background: #fff; border: 1px solid var(--line); border-radius: 9px; display: flex; align-items: center; cursor: pointer; }
.active-color-chip:hover { border-color: #bbb7ad; }
.active-color-chip i { width: 29px; height: 100%; flex: 0 0 auto; display: block; border-radius: 6px; background: var(--orange); box-shadow: inset 0 2px 4px rgba(255,255,255,.35), inset 0 -2px 4px rgba(0,0,0,.1); }
.active-color-chip svg { width: 13px; height: 13px; margin-left: 3px; fill: none; stroke: #777871; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
#mardMatcherInput { position: absolute; width: 0; height: 0; opacity: 0; }

.palette-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.palette-heading .panel-label { padding-left: 0; }
.palette-heading > span { color: #9b9a93; font: 9px "DM Mono", monospace; }
.palette-tabs { background: #f0ede6; border-radius: 8px; padding: 3px; display: grid; grid-template-columns: repeat(9, 1fr); gap: 2px; }
.palette-tabs button { height: 27px; padding: 0; border: 0; background: transparent; border-radius: 6px; font-size: 10px; color: #98978f; cursor: pointer; }
.palette-tabs button.active { background: #fffdfa; color: #50524d; font-weight: 700; box-shadow: 0 1px 4px rgba(57,53,44,.1); }
.palette-search { height: 34px; margin-top: 9px; padding: 0 9px; display: flex; align-items: center; gap: 7px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.palette-search:focus-within { border-color: #bdb9af; box-shadow: 0 0 0 3px rgba(239,119,70,.12); }
.palette-search svg { width: 14px; height: 14px; flex: 0 0 auto; fill: none; stroke: #9a9992; stroke-width: 1.8; stroke-linecap: round; }
.palette-search input { min-width: 0; width: 100%; height: 100%; padding: 0; border: 0; outline: 0; background: transparent; color: var(--ink); font-size: 10px; }
.palette-search input::placeholder { color: #aaa9a2; }
.color-palette { display: grid; grid-template-columns: repeat(6, 1fr); gap: 7px 5px; margin-top: 11px; max-height: 270px; overflow-y: auto; padding: 4px 3px 6px; scrollbar-width: thin; }
.swatch { height: 38px; min-width: 0; padding: 0; border: 0; background: transparent; color: #81827b; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: space-between; }
.swatch i { width: 24px; height: 24px; flex: 0 0 auto; display: block; border-radius: 50%; background: var(--swatch); border: 2px solid rgba(255,255,255,.92); box-shadow: 0 0 0 1px rgba(99,95,85,.18), inset 0 -3px 5px rgba(0,0,0,.1), inset 0 2px 4px rgba(255,255,255,.35); transition: transform .15s ease, box-shadow .15s ease; }
.swatch small { font: 8px "DM Mono", monospace; line-height: 1; }
.swatch:hover i { transform: scale(1.1); }
.swatch.active { color: var(--ink); font-weight: 700; }
.swatch.active i { box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--ink), inset 0 -3px 5px rgba(0,0,0,.1); transform: scale(.84); }
.empty-palette { padding: 24px 0 12px; color: #a3a29b; font-size: 10px; text-align: center; }

.color-panel .panel-divider { margin: 19px 0 15px; }
.usage-heading { display: flex; justify-content: space-between; align-items: center; }
.usage-heading .panel-label { padding-left: 0; }
.usage-heading span { display: block; font-size: 9px; color: #a4a39c; margin-top: 4px; }
.usage-actions { display: flex; align-items: center; gap: 5px; }
.mini-btn { height: 25px; padding: 0 9px; border: 1px solid var(--line); background: transparent; color: #888983; border-radius: 6px; font-size: 9px; cursor: pointer; }
.mini-btn:hover { color: var(--ink); background: white; }
.usage-list { margin-top: 11px; overflow-y: auto; min-height: 0; scrollbar-width: thin; }
.usage-item { height: 34px; display: grid; grid-template-columns: 17px 1fr auto; align-items: center; gap: 8px; border-bottom: 1px dashed rgba(210,206,196,.65); font-size: 10px; }
.usage-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--usage-color); box-shadow: inset 0 -2px 3px rgba(0,0,0,.12); }
.usage-name { color: #656660; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.usage-name small { margin-left: 4px; color: #aaa9a2; font: 8px "DM Mono", monospace; }
.usage-count { color: #93938c; font-family: "DM Mono", monospace; }
.usage-count b { color: #565853; }
.empty-usage { margin: 24px auto; color: #a3a29b; font-size: 10px; text-align: center; }
.empty-usage span { display: block; font-size: 28px; color: #cfccc4; }
.clear-button { flex: 0 0 36px; width: 100%; margin-top: auto; border-radius: 8px; border: 1px solid #e4d8d0; color: #b56d52; background: #fffaf7; font-size: 10px; display: flex; align-items: center; justify-content: center; gap: 6px; cursor: pointer; }
.clear-button:hover { background: #fff2eb; border-color: #e6c2b4; }
.clear-button svg { width: 14px; }

.modal { width: min(470px, calc(100vw - 32px)); border: 0; padding: 0; border-radius: 20px; box-shadow: 0 30px 80px rgba(38, 37, 32, .28); background: var(--surface); }
.modal::backdrop { background: rgba(37,39,36,.42); backdrop-filter: blur(4px); }
.modal form { padding: 30px; position: relative; }
.modal-close { position: absolute; right: 18px; top: 16px; width: 31px; height: 31px; border: 0; background: transparent; color: #989890; font-size: 24px; cursor: pointer; border-radius: 8px; }
.modal-close:hover { background: #f1eee7; }
.modal-icon { width: 44px; height: 44px; border-radius: 12px; background: var(--orange-soft); color: var(--orange); display: grid; place-items: center; }
.modal-icon svg { width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.7; }
.modal h2 { font-size: 20px; margin: 15px 0 4px; }
.modal p { color: var(--muted); font-size: 12px; margin: 0 0 23px; }
.field-label { display: block; color: #777871; font-size: 11px; font-weight: 700; margin: 14px 0 8px; }
.field-label-row { display: flex; align-items: center; justify-content: space-between; }
.field-label-row label { cursor: pointer; }
.field-label-row output { color: #cf5f34; font: 600 12px "DM Mono", monospace; }
.text-input { width: 100%; height: 42px; border: 1px solid var(--line); border-radius: 9px; background: white; padding: 0 12px; font-size: 13px; color: var(--ink); }
.size-editor { display: grid; grid-template-columns: 1fr 86px; align-items: center; gap: 16px; }
.size-editor > input[type="range"] { width: 100%; accent-color: var(--orange); cursor: pointer; }
.size-number { height: 42px; display: grid; grid-template-columns: 1fr auto; align-items: center; border: 1px solid var(--line); border-radius: 9px; background: #fff; overflow: hidden; }
.size-number:focus-within { outline: 3px solid rgba(239, 119, 70, .25); outline-offset: 2px; }
.size-number input { min-width: 0; width: 100%; height: 100%; padding: 0 0 0 10px; border: 0; outline: 0; color: var(--ink); font: 500 12px "DM Mono", monospace; }
.size-number span { padding-right: 10px; color: #999890; font-size: 10px; }
.field-hint { margin: 8px 0 0 !important; font-size: 10px !important; }
.modal-small { width: min(430px, calc(100vw - 32px)); }
.modal-actions { margin-top: 26px; display: flex; justify-content: flex-end; gap: 8px; }

.toast { position: fixed; left: 50%; bottom: 30px; transform: translate(-50%, 15px); padding: 10px 15px; border-radius: 9px; color: white; background: rgba(39,43,41,.94); box-shadow: var(--shadow); font-size: 11px; opacity: 0; pointer-events: none; transition: .25s ease; z-index: 50; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.svg-sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

@media (max-width: 1050px) {
  .workspace { grid-template-columns: 178px minmax(430px, 1fr) 220px; gap: 10px; padding: 10px; }
  .topbar { padding: 0 18px; }
  .brand-tagline { display: none; }
  .color-palette { gap: 8px 6px; }
}

@media (max-width: 1480px) {
  .project-file-btn { width: 40px; padding: 0; }
  .project-file-btn span { display: none; }
}

@media (max-width: 820px) {
  body { overflow: auto; }
  .app-shell { height: auto; }
  .topbar { grid-template-columns: 1fr auto; height: 68px; padding: 0 14px; }
  .project-title-wrap { display: none; }
  #newProjectBtn { display: none; }
  .workspace { display: flex; flex-direction: column; height: auto; padding: 10px; }
  .canvas-section { order: 1; height: min(70vh, 650px); }
  .tool-panel { order: 2; padding: 12px; }
  .color-panel { order: 3; padding: 16px; }
  .tool-list { grid-template-columns: repeat(5, 1fr); }
  .tool { height: 58px; display: flex; flex-direction: column; justify-content: center; gap: 4px; padding: 0; font-size: 10px; }
  .tool kbd, .tool-panel > .panel-label, .tool-panel .panel-divider, .tool-panel .compact-actions, .tool-panel .toggle-row, .panel-tip { display: none; }
  .color-palette { grid-template-columns: repeat(10, 1fr); }
  .usage-list { max-height: 180px; }
  .canvas-viewport { padding: 34px; }
}

@media (max-width: 520px) {
  .topbar { gap: 8px; }
  .top-actions { gap: 5px; }
  .top-actions .btn span { display: none; }
  .top-actions .btn { width: 40px; padding: 0; }
  .brand-mark { width: 34px; height: 34px; margin-right: 6px; }
  .brand-name { font-size: 18px; }
  .board-kind, .footer-help { display: none; }
  .canvas-footer { height: 64px; flex: 0 0 64px; flex-wrap: wrap; align-content: center; row-gap: 5px; }
  .canvas-footer .footer-spacer { flex-basis: 50%; }
  .filing-links { width: 100%; margin-left: 0; gap: 10px; }
  .resize-button { width: 27px; padding: 0; justify-content: center; font-size: 0; }
  .zoom-control input { width: 55px; }
  .color-palette { grid-template-columns: repeat(8, 1fr); }
  .canvas-viewport { padding: 30px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
