@font-face {
  font-family: "Drawin Sans";
  src: url("./fonts/source-sans-3-400.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Drawin Sans";
  src: url("./fonts/source-sans-3-600.woff2") format("woff2");
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Drawin Serif";
  src: url("./fonts/source-serif-4-600.woff2") format("woff2");
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}

:root {
  --ink: #161513;
  --ink-2: #24221e;
  --raised: #312e28;
  --line: #454037;
  --paper: #f4f0e6;
  --fg: #f3efe6;
  --muted: #a39c90;
  --brass: #d08a2d;
  --brass-2: #f0c27a;
  --danger: #a33b32;
  --hair: rgba(243, 239, 230, 0.08);
  --shadow: 0 0 0 1px rgba(243, 239, 230, 0.08), 0 24px 48px rgba(0, 0, 0, 0.45);
  --sans: "Drawin Sans", "Segoe UI", sans-serif;
  --serif: "Drawin Serif", Georgia, serif;
  --tool: 48px;
  --bar: 52px;
  --status: 32px;
  --rail: 84px;
  --side: 300px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--ink); color: var(--fg); }
body {
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
  overflow: hidden;
  user-select: none;
}
input, textarea { user-select: text; }
button, input, select, textarea { font: inherit; color: inherit; }
button, #paint-link { cursor: pointer; }
:focus-visible {
  outline: 2px solid var(--brass);
  outline-offset: 2px;
}
.tool, .mini, .menu-btn, .swatch, .layer-btn, .obj, #menu button, #dialog-ok, #dialog-cancel {
  transition: background-color 150ms cubic-bezier(0.22, 1, 0.36, 1), border-color 150ms cubic-bezier(0.22, 1, 0.36, 1), color 150ms cubic-bezier(0.22, 1, 0.36, 1), transform 80ms cubic-bezier(0.22, 1, 0.36, 1);
}
.tool:active, .mini:active, #menu button:active, #dialog-ok:active { transform: scale(0.98); }
.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

#app {
  height: 100%;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: var(--rail) minmax(0, 1fr) var(--side);
  grid-template-rows: var(--bar) minmax(0, 1fr) var(--status);
  grid-template-areas:
    "bar bar bar"
    "tools stage inspector"
    "status status status";
  background: var(--ink);
}

#bar {
  grid-area: bar;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 8px;
  min-width: 0;
  height: var(--bar);
  padding: 0 10px;
  overflow: hidden;
  border-bottom: 1px solid var(--line);
  background: var(--ink-2);
}
.brand {
  font-family: var(--serif);
  font-size: 22px;
  letter-spacing: -0.03em;
  padding: 0 8px 0 4px;
  white-space: nowrap;
}
.brand em {
  font-style: normal;
  color: var(--brass-2);
  font-size: 13px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  margin-left: 6px;
  font-family: var(--sans);
  font-weight: 600;
}
#menus {
  display: flex;
  gap: 2px;
  flex: 1 1 auto;
  min-width: 0;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}
#menus::-webkit-scrollbar { display: none; }
.menu-btn { flex: 0 0 auto; letter-spacing: 0.01em; color: var(--muted); }
.menu-btn[aria-expanded="true"] { background: var(--raised); color: var(--brass-2); }
.menu-btn, #paint-link, .dialog-actions button, .swatch, .mini, .layer-btn {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  min-height: 44px;
  min-width: 44px;
  padding: 0 10px;
  color: var(--fg);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.menu-btn:hover, #paint-link:hover, .mini:hover, .layer-btn:hover { background: var(--raised); color: var(--fg); }
#paint-link {
  border-color: var(--line);
  color: var(--brass-2);
  font-weight: 600;
}
.docname { margin-left: auto; flex: 0 0 auto; }
#panel-toggle {
  display: none;
  flex: 0 0 auto;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--fg);
  border-radius: 999px;
  min-height: 44px;
  padding: 0 12px;
}
body.panel-open #panel-toggle {
  background: var(--brass);
  color: var(--ink);
  border-color: transparent;
  font-weight: 600;
}
#doc-name {
  background: var(--ink);
  border: 0;
  box-shadow: inset 0 0 0 1px var(--hair);
  border-radius: 8px;
  min-height: 44px;
  padding: 0 10px;
  width: min(220px, 28vw);
  color: var(--fg);
}
#doc-name:focus { outline: 2px solid var(--brass); outline-offset: 1px; }

#tools {
  grid-area: tools;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
  min-width: 0;
  padding: 8px;
  border-right: 1px solid var(--line);
  background: var(--ink-2);
  overflow: auto;
}
.tool {
  width: 100%;
  min-width: 44px;
  min-height: 44px;
  height: auto;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--fg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 4px 2px;
}
.tool .tw {
  display: block;
  font-size: 10px;
  line-height: 1.05;
  text-align: center;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tool svg { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.tool[aria-pressed="true"] {
  background: var(--brass);
  color: var(--ink);
  border-color: transparent;
  box-shadow: inset 0 1px 0 rgba(255, 248, 230, 0.45), 0 1px 1px rgba(0, 0, 0, 0.35);
}
.tool:hover { background: var(--raised); }
.tool[aria-pressed="true"]:hover { background: var(--brass-2); }
#tools.can-scroll {
  -webkit-mask-image: linear-gradient(#000 0, #000 calc(100% - 18px), transparent);
  mask-image: linear-gradient(#000 0, #000 calc(100% - 18px), transparent);
}
.tool[aria-pressed="true"]:hover { background: var(--brass-2); }

#stage-wrap {
  grid-area: stage;
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  grid-template-rows: 22px minmax(0, 1fr);
}
#ruler-corner {
  position: relative;
  background: var(--ink);
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
#ruler-corner::after {
  content: "";
  position: absolute;
  right: 4px;
  bottom: 4px;
  width: 7px;
  height: 7px;
  border-right: 1.5px solid var(--brass);
  border-bottom: 1.5px solid var(--brass);
}
#ruler-x { grid-column: 2; width: 100%; height: 22px; background: var(--ink); border-bottom: 1px solid var(--line); cursor: ns-resize; }
#ruler-y { grid-row: 2; width: 22px; height: 100%; background: var(--ink); border-right: 1px solid var(--line); cursor: ew-resize; }
#stage {
  position: relative;
  overflow: hidden;
  background-color: #100f0d;
  background-image:
    radial-gradient(ellipse at 50% 46%, rgba(244, 236, 214, 0.05), transparent 58%),
    linear-gradient(180deg, #1a1916, #0e0d0c);
  touch-action: none;
  cursor: crosshair;
}
#stage.tool-select { cursor: default; }
#stage.tool-hand, #stage.panning { cursor: grab; }
#svg { width: 100%; height: 100%; display: block; }
.sheet {
  filter:
    drop-shadow(0 1px 0 rgba(255, 250, 240, 0.55))
    drop-shadow(0 2px 3px rgba(0, 0, 0, 0.28))
    drop-shadow(0 22px 36px rgba(0, 0, 0, 0.42));
}
#overlay { pointer-events: none; }
#overlay .handle, #overlay .guide, #overlay .gstop, #overlay .node { pointer-events: all; }
.handle { fill: var(--paper); stroke: var(--brass); }
.rot { fill: var(--brass); }
.selbox { fill: none; stroke: var(--brass); }
.guide { stroke: #5aa0c8; }
.node { fill: var(--paper); stroke: var(--ink); cursor: pointer; }
.node.on { fill: var(--brass); }
.penprev { fill: none; stroke: var(--brass); }
#empty {
  position: absolute;
  left: 50%;
  top: 46%;
  transform: translate(-50%, -50%);
  margin: 0;
  color: #8d867a;
  pointer-events: none;
  text-align: center;
  max-width: 280px;
  font-family: var(--serif);
  font-size: 28px;
  letter-spacing: -0.03em;
  line-height: 1.15;
}
#empty span {
  display: block;
  margin-top: 8px;
  font-family: var(--sans);
  font-size: 13px;
  letter-spacing: 0.01em;
  line-height: 1.45;
  color: var(--muted);
}

#inspector {
  grid-area: inspector;
  border-left: 1px solid var(--line);
  background: var(--ink-2);
  overflow: auto;
  padding: 12px;
}
#inspector h3 {
  margin: 16px 0 8px;
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
}
#inspector h3::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--hair);
}
#inspector h3:first-child { margin-top: 0; }
.row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.row label { flex: 1; display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
.row input[type="number"], .row input[type="text"], .row select, #inspector textarea {
  background: var(--ink);
  border: 0;
  box-shadow: inset 0 0 0 1px var(--hair);
  border-radius: 8px;
  min-height: 44px;
  padding: 0 8px;
  width: 100%;
  color: var(--fg);
}
#inspector textarea { min-height: 72px; padding: 8px; resize: vertical; }
input[type="color"] {
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--line);
  background: var(--ink);
  border-radius: 8px;
}
.swatches { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.swatch {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--line);
}
html[data-kp-size="L"] .mini,
html[data-kp-size="XL"] .mini,
html[data-kp-size="L"] select,
html[data-kp-size="XL"] select,
html[data-kp-size="L"] .swatch,
html[data-kp-size="XL"] .swatch,
html[data-kp-size="L"] .layer-btn,
html[data-kp-size="XL"] .layer-btn {
  min-width: 44px;
  min-height: 44px;
}
html[data-kp-size="L"],
html[data-kp-size="XL"],
html[data-kp-lang="ar"] { --rail: 96px; }
html[data-kp-size="L"] .tool .tw,
html[data-kp-size="XL"] .tool .tw,
html[data-kp-lang="ar"] .tool .tw {
  display: block;
  font-size: .75rem;
  line-height: 1.1;
  text-align: center;
}
html[data-kp-lang="ar"] .ds-ar { display: block !important; }
.tool { height: auto; min-height: 44px; }
.swatch { forced-color-adjust: none; color: #000; font-weight: 800; }
html[data-kp-contrast="1"] #bar,
html[data-kp-contrast="1"] #tools,
html[data-kp-contrast="1"] #inspector { background: #000; color: #ffe14a; }
#app { overflow: hidden; }
.actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin-bottom: 8px;
}
.actions.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.mini {
  flex: 1;
  border: 0;
  box-shadow: inset 0 0 0 1px var(--hair);
  min-height: 44px;
  background: var(--ink);
  white-space: nowrap;
  padding: 0 6px;
  font-size: 12px;
}
.mini:hover { box-shadow: inset 0 0 0 1px var(--brass); color: var(--fg); }
.layer {
  display: grid;
  grid-template-columns: 44px 44px minmax(0, 1fr) 44px;
  gap: 4px;
  align-items: center;
  margin-bottom: 4px;
}
.layer.on { background: var(--raised); border-radius: 8px; }
.layer button { min-height: 44px; min-width: 44px; }
.layer-btn svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.objs { display: flex; flex-direction: column; gap: 2px; margin: 4px 0 8px; max-height: 200px; overflow: auto; }
.obj {
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 8px;
  min-height: 44px;
  padding: 0 8px;
  color: var(--fg);
}
.obj.on { background: var(--raised); color: var(--brass-2); }
.check { display: flex; gap: 8px; align-items: center; color: var(--fg); margin: 6px 0; }

#status {
  grid-area: status;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 0 12px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  background: var(--ink);
  font-size: 12px;
}
#hint { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
#coords {
  white-space: nowrap;
  color: var(--brass-2);
  font-variant-numeric: tabular-nums;
  font-size: 11px;
  letter-spacing: 0.06em;
}

#menu {
  position: fixed;
  z-index: 20;
  min-width: min(260px, calc(100vw - 16px));
  max-height: min(70vh, 480px);
  overflow: auto;
  background: #1e1c19;
  border: 0;
  border-radius: 14px;
  padding: 6px;
  box-shadow: var(--shadow);
  transform-origin: top left;
  animation: menu-in 150ms cubic-bezier(0.22, 1, 0.36, 1);
  scrollbar-width: thin;
  scrollbar-color: #454037 transparent;
}
@keyframes menu-in {
  from { opacity: 0; transform: translateY(-4px) scale(0.98); }
  to { opacity: 1; transform: none; }
}
#menu button {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 8px;
  min-height: 44px;
  padding: 0 10px;
  display: flex;
  justify-content: space-between;
  gap: 16px;
}
#menu button:hover { background: var(--raised); }
#menu .sep { height: 1px; margin: 5px 8px; background: var(--hair); }
#menu kbd {
  color: var(--brass-2);
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: 0.04em;
  background: rgba(208, 138, 45, 0.14);
  border-radius: 6px;
  padding: 2px 6px;
}

#dialog {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(12, 11, 9, 0.55);
  backdrop-filter: blur(10px);
  display: grid;
  place-items: center;
  padding: 16px;
}
#dialog[hidden] { display: none; }
#dialog form {
  width: min(420px, 100%);
  background: #1e1c19;
  border: 0;
  border-radius: 24px;
  padding: 18px;
  box-shadow: var(--shadow);
  animation: dialog-in 200ms cubic-bezier(0.22, 1, 0.36, 1);
}
@keyframes dialog-in {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: none; }
}
#dialog h2 { font-family: var(--serif); margin: 0 0 12px; font-size: 26px; }
#dialog-body label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; color: var(--muted); }
#dialog-body input {
  min-height: 44px;
  border-radius: 8px;
  border: 0;
  box-shadow: inset 0 0 0 1px var(--hair);
  background: var(--ink);
  padding: 0 10px;
  color: var(--fg);
}
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-actions button {
  border: 1px solid var(--line);
  background: var(--ink);
  min-height: 44px;
  padding: 0 14px;
  border-radius: 8px;
}
#dialog-ok { background: var(--brass); color: var(--ink); border-color: transparent; font-weight: 600; }

body.hub .brand,
body.hub #paint-link { display: none !important; }

@media (max-width: 900px), (max-height: 520px) {
  #app {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
  }
  #bar {
    flex: 0 0 44px;
    height: 44px;
    max-height: 44px;
    padding: 0 8px;
    gap: 6px;
  }
  #tools {
    flex: 0 0 48px;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    height: 48px;
    min-height: 48px;
    max-height: 48px;
    padding: 2px 6px;
    gap: 2px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    overflow-x: auto;
    overflow-y: hidden;
  }
  #stage-wrap { flex: 1 1 auto; min-height: 80px; min-width: 0; }
  #inspector {
    flex: 0 1 200px;
    max-height: 34%;
    min-height: 0;
    border-left: 0;
    border-top: 1px solid var(--line);
    overflow: auto;
  }
  #status { flex: 0 0 28px; min-height: 28px; }
  #panel-toggle { display: inline-flex; }
  body:not(.panel-open) #inspector { display: none; }
  .brand { font-size: 18px; padding: 0; }
  .brand em, #paint-link { display: none !important; }
  #doc-name { width: 108px; }
  .tool { min-width: 44px; min-height: 44px; width: auto; height: auto; flex: 0 0 auto; }
  #tools.can-scroll {
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 22px), transparent);
    mask-image: linear-gradient(to right, #000 calc(100% - 22px), transparent);
  }
}

@media (max-width: 900px) and (max-height: 560px), (max-height: 420px) {
  #bar { flex-basis: 48px; height: 48px; max-height: none; min-height: 48px; }
  #tools, #tools.can-scroll { flex-basis: 56px; height: auto; min-height: 56px; max-height: none; }
  .tool { width: auto; min-width: 44px; height: auto; min-height: 44px; flex-basis: auto; }
  #status { display: none; }
  #inspector { flex-basis: 120px; max-height: 120px; }
}

@media (max-width: 560px) {
  .docname { display: none; }
  .menu-btn { padding: 0 7px; font-size: 13px; }
  #panel-toggle { padding: 0 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto; transition: none !important; animation: none !important; }
}
