:root {
  --ink: #20211d;
  --muted: #72746e;
  --paper: #f5f3e9;
  --panel: #fffefa;
  --line: #deddd5;
  --accent: #d9ff45;
  --red: #a43d32;
  --shadow: 0 18px 55px rgba(33, 35, 28, .12);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  overflow-x: hidden;
  color: var(--ink);
  background: #eeede7;
  font: 14px/1.45 Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input { font-family: inherit; }
button { border: 0; cursor: pointer; font-weight: 700; touch-action: manipulation; }
input {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: white;
  color: var(--ink);
  font-size: 16px;
}
input:focus-visible, button:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

.topbar {
  position: relative;
  z-index: 20;
  min-height: 72px;
  padding: 10px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand, .device-state { display: flex; align-items: center; gap: 12px; }
.brand strong, .brand span { display: block; }
.brand span { color: var(--muted); font-size: 12px; }
.mark {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: var(--ink);
  color: var(--accent);
  font: 700 24px Georgia;
}
.mark.small { width: 38px; height: 38px; border-radius: 9px; font-size: 20px; }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: #5fa63d; box-shadow: 0 0 0 4px #e4f2dc; }

.workspace { min-height: calc(100vh - 72px); display: grid; grid-template-columns: 224px minmax(580px, 1fr) 250px; }
.sidebar { padding: 24px 18px; background: var(--panel); border-right: 1px solid var(--line); }
.inspector { border-right: 0; border-left: 1px solid var(--line); }
.sidebar section + section { margin-top: 34px; }
.eyebrow { margin: 0 0 12px; color: var(--muted); font-size: 10px; font-weight: 800; letter-spacing: .15em; }

.screen-list, .palette { display: grid; gap: 7px; }
.screen-row, .palette-item, .add-button { min-height: 44px; }
.screen-row {
  width: 100%;
  padding: 9px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: none;
  color: var(--ink);
  text-align: left;
}
.screen-row:hover { background: #f3f2ec; }
.screen-row.active { border-color: var(--ink); background: var(--paper); }
.screen-row .dot { flex: 0 0 auto; width: 7px; height: 7px; border-radius: 50%; background: #c4c5be; }
.screen-row.published .dot { background: #5fa63d; }
.screen-row span:nth-child(2) { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.add-button { width: 100%; margin-top: 8px; padding: 9px; border: 1px dashed #b8b9b1; border-radius: 8px; background: transparent; color: var(--muted); }
.palette-item {
  padding: 8px 10px;
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #faf9f4;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.palette-item:active { cursor: grabbing; transform: scale(.985); }
.palette-icon { width: 30px; height: 30px; display: grid; place-items: center; flex: 0 0 auto; border-radius: 6px; background: var(--ink); color: var(--paper); font-size: 12px; }
.palette-copy { min-width: 0; }
.palette-copy strong, .palette-copy small { display: block; }
.palette-copy small { color: var(--muted); font-size: 10px; font-weight: 500; }

.stage-column { min-width: 0; padding: 24px 28px 18px; }
.stage-toolbar, .canvas-footer { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.stage-toolbar { margin-bottom: 18px; }
.stage-toolbar > div { display: flex; align-items: center; gap: 9px; }
.screen-name { width: min(330px, 34vw); padding: 4px; border-width: 0 0 1px; border-color: transparent; border-radius: 0; background: none; font-size: 22px; font-weight: 700; }
.screen-name:focus { outline: 0; border-color: var(--ink); }
#save-state { color: var(--muted); font-size: 12px; white-space: nowrap; }
.toolbar-actions { flex-wrap: wrap; justify-content: flex-end; }
.primary, .secondary { min-height: 40px; padding: 9px 13px; border-radius: 7px; }
.primary { background: var(--ink); color: white; }
.primary:hover { background: #3a3c35; }
.secondary { background: var(--accent); color: var(--ink); }
.quiet { min-height: 40px; padding: 7px 9px; background: transparent; color: var(--muted); }
.quiet:hover { color: var(--ink); }

.device-shell { max-width: 832px; margin: auto; padding: 16px; border-radius: 18px; background: #d7d6d0; box-shadow: var(--shadow), inset 0 1px white; }
.canvas { position: relative; overflow: hidden; width: 100%; aspect-ratio: 5 / 3; background: #faf9ef; border: 2px solid #24251f; touch-action: none; }
.canvas img { display: block; width: 100%; height: 100%; image-rendering: pixelated; pointer-events: none; }
.widget-handle { position: absolute; min-width: 8px; min-height: 8px; border: 1.5px solid transparent; cursor: move; touch-action: none; }
.widget-handle:hover { border-color: rgba(32, 33, 29, .4); }
.widget-handle.selected { border-color: var(--accent); box-shadow: 0 0 0 1px #20211d; }
.widget-handle .tag { position: absolute; left: -1px; top: -20px; display: none; padding: 2px 5px; background: var(--ink); color: white; font-size: 9px; white-space: nowrap; }
.widget-handle.selected .tag { display: block; }
.resize-grip { position: absolute; right: -6px; bottom: -6px; display: none; width: 16px; height: 16px; border: 2px solid var(--ink); border-radius: 3px; background: var(--accent); cursor: nwse-resize; touch-action: none; }
.widget-handle.selected .resize-grip { display: block; }
.canvas-footer { max-width: 832px; margin: 10px auto 0; color: var(--muted); font-size: 11px; }
.canvas-footer span + span { flex: 1; text-align: center; }

.inspector h2 { margin: 0 0 16px; font-size: 18px; }
.empty { padding: 18px 12px; border: 1px dashed var(--line); border-radius: 8px; color: var(--muted); }
label { display: grid; gap: 5px; color: var(--muted); font-size: 11px; font-weight: 700; }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field-grid label:last-child { grid-column: 1 / -1; }
#text-fields { margin-top: 10px; }
#text-fields label + label { margin-top: 10px; }
.danger { min-height: 44px; margin-top: 14px; padding: 7px 0; background: transparent; color: var(--red); }
dl { margin: 0; }
dl div { padding: 8px 0; display: flex; justify-content: space-between; gap: 8px; border-bottom: 1px solid var(--line); }
dt { color: var(--muted); }
dd { margin: 0; font-weight: 700; text-align: right; }
.note { padding: 13px; border-radius: 9px; background: var(--paper); }
.note p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }

.login-page { min-height: 100vh; min-height: 100dvh; padding: 16px; display: grid; place-items: center; background: linear-gradient(135deg, #e8e7df, #f7f6f0); }
.login-card { width: min(420px, 100%); padding: 36px; border: 1px solid var(--line); border-radius: 16px; background: var(--panel); box-shadow: var(--shadow); }
.login-card h1 { margin: 16px 0 4px; font-size: 32px; }
.login-card .muted { margin: 0 0 24px; color: var(--muted); }
.login-card form { display: grid; gap: 12px; }
.login-card .primary { margin-top: 4px; }
.error { margin: 0; color: var(--red); }
#toast { position: fixed; right: 22px; bottom: 22px; z-index: 50; max-width: calc(100vw - 44px); padding: 10px 14px; transform: translateY(80px); opacity: 0; border-radius: 8px; background: var(--ink); color: white; transition: .2s; pointer-events: none; }
#toast.show { transform: none; opacity: 1; }

@media (max-width: 1050px) {
  .workspace { grid-template-columns: 190px minmax(520px, 1fr); }
  .inspector { grid-column: 1 / -1; display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 20px; border-top: 1px solid var(--line); border-left: 0; }
  .inspector section + section { margin-top: 0; }
}

@media (max-width: 760px) {
  body { padding-bottom: env(safe-area-inset-bottom); }
  .topbar { position: sticky; top: 0; min-height: 60px; padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left)); }
  .brand span { display: none; }
  .device-state { gap: 5px; }
  .device-state > span:not(.pulse) { display: none; }
  .mark.small { width: 36px; height: 36px; }
  .workspace { display: flex; flex-direction: column; min-height: auto; }
  .stage-column { order: 1; padding: 12px 8px 16px; }
  .left-panel { order: 2; }
  .inspector { order: 3; }
  .sidebar { padding: 20px max(14px, env(safe-area-inset-right)) 20px max(14px, env(safe-area-inset-left)); border: 0; border-top: 1px solid var(--line); }
  .left-panel { display: block; }
  .left-panel section + section { margin-top: 22px; }
  .stage-toolbar { margin: 0 4px 12px; display: block; }
  .stage-toolbar > div:first-child { margin-bottom: 10px; }
  .screen-name { width: 100%; min-width: 0; font-size: 20px; }
  .toolbar-actions { display: grid !important; grid-template-columns: auto 1fr 1.35fr; gap: 6px !important; }
  .toolbar-actions button { min-height: 44px; padding-inline: 8px; }
  .device-shell { width: 100%; padding: 6px; border-radius: 9px; }
  .canvas { border-width: 1px; }
  .resize-grip { right: -10px; bottom: -10px; width: 24px; height: 24px; border-width: 2px; }
  .widget-handle .tag { top: -22px; min-height: 20px; font-size: 10px; }
  .canvas-footer { min-height: 40px; margin: 5px 2px 0; }
  .canvas-footer span:nth-child(2) { display: none; }
  .canvas-footer .quiet { margin-left: auto; }
  .screen-list, .palette { margin-inline: -14px; padding: 0 14px 5px; display: flex; gap: 8px; overflow-x: auto; scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .screen-list::-webkit-scrollbar, .palette::-webkit-scrollbar { display: none; }
  .screen-row { flex: 0 0 min(190px, 66vw); scroll-snap-align: start; border-color: var(--line); background: #faf9f4; }
  .palette-item { flex: 0 0 160px; scroll-snap-align: start; }
  .add-button { min-height: 44px; }
  .inspector { display: block; }
  .inspector section + section { margin-top: 22px; }
  .field-grid { gap: 12px; }
  .field-grid label:last-child { grid-column: auto; }
  #toast { right: 12px; bottom: max(12px, env(safe-area-inset-bottom)); max-width: calc(100vw - 24px); }
  .login-card { padding: 28px 22px; }
  .login-card .primary { min-height: 48px; font-size: 16px; }
}

@media (max-width: 390px) {
  .toolbar-actions { grid-template-columns: .8fr .8fr 1.4fr; }
  .toolbar-actions button { font-size: 12px; }
  .canvas-footer > span:first-child { display: none; }
}
