:root{
  --bg:#f6f8fd;
  --panel:#ffffff;
  --line:#d8ddea;
  --text:#1f2430;
  --muted:#6b7384;
  --accent:#2a5bd7;
  --danger:#b42318;
  --danger-bg:#fff1f0;
  --shadow:0 10px 28px rgba(27,39,94,.10);
  --safe-bottom:calc(env(safe-area-inset-bottom, 0px) + 12px);
}

*{box-sizing:border-box}

html,body{
  height:100%;
  margin:0;
  overflow:hidden;
  background:var(--bg);
  color:var(--text);
  font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Hiragino Kaku Gothic ProN","Yu Gothic",sans-serif;
}

body{
  display:flex;
  flex-direction:column;
}

button,
input{
  font:inherit;
}

.app{
  position:relative;
  flex:1;
  min-height:0;
  display:flex;
  flex-direction:column;
  gap:12px;
  padding:12px 12px var(--safe-bottom);
}

.topbar{
  display:grid;
  grid-template-columns:104px 1fr 104px;
  gap:10px;
  align-items:center;
  z-index:10;
}

.topbar-title{
  margin:0;
  text-align:center;
  font-size:18px;
  font-weight:800;
  letter-spacing:.02em;
}

.btn{
  border:1px solid var(--line);
  border-radius:14px;
  background:var(--panel);
  color:var(--text);
  min-height:44px;
  padding:0 12px;
  box-shadow:var(--shadow);
  cursor:pointer;
  white-space:nowrap;
}

.btn:active{
  transform:translateY(1px);
}

.stage-wrap{
  position:relative;
  flex:1;
  min-height:0;
  background:var(--panel);
  border:1px solid var(--line);
  border-radius:24px;
  box-shadow:var(--shadow);
  overflow:hidden;
}

.status{
  position:absolute;
  top:12px;
  left:12px;
  right:12px;
  z-index:3;
  display:none;
  padding:10px 12px;
  border-radius:14px;
  background:rgba(255,255,255,.94);
  border:1px solid #e4e8f3;
  font-size:13px;
  line-height:1.5;
  color:#3153a4;
  backdrop-filter:blur(10px);
  pointer-events:none;
}

.status.error{
  display:block;
  color:var(--danger);
  background:var(--danger-bg);
  border-color:#f2c6bf;
}

.stage{
  width:100%;
  height:100%;
  display:block;
  background:#fbfcff;
}

.draw-katex-label{
  display:flex;
  align-items:center;
  justify-content:center;
  width:100%;
  height:100%;
  color:inherit;
  font-weight:800;
  line-height:1;
  white-space:nowrap;
  pointer-events:none;
  text-shadow:
    0 0 4px #fff,
    0 0 4px #fff,
    0 0 7px #fff;
}

.draw-katex-label .katex{
  color:inherit;
  font-size:1em;
  line-height:1;
  white-space:nowrap;
}

.draw-katex-measure{
  position:absolute;
  left:-10000px;
  top:-10000px;
  display:inline-flex;
  width:auto;
  height:auto;
  white-space:nowrap;
  visibility:hidden;
  pointer-events:none;
}

.bottom-bar{
  display:grid;
  grid-template-columns:repeat(3,minmax(0,1fr));
  gap:10px;
  z-index:10;
}

.bottom-bar.one-field{
  grid-template-columns:minmax(0,min(100%,360px));
  justify-content:center;
}

.bottom-bar.two-fields{
  grid-template-columns:repeat(2,minmax(0,1fr));
}

.bottom-bar.four-fields{
  grid-template-columns:repeat(4,minmax(0,1fr));
}

.bottom-bar.five-fields{
  grid-template-columns:repeat(5,minmax(0,1fr));
}

.bottom-bar.seven-fields{
  grid-template-columns:repeat(7,minmax(0,1fr));
}

.field{
  display:grid;
  gap:4px;
  min-width:0;
}

.field-label{
  font-size:11px;
  color:var(--muted);
  text-align:center;
}

.field-input{
  width:100%;
  min-height:46px;
  border:1px solid var(--line);
  border-radius:14px;
  padding:0 12px;
  text-align:center;
  background:var(--panel);
  box-shadow:var(--shadow);
}

.sheet-backdrop{
  position:fixed;
  inset:0;
  background:rgba(15,23,42,.18);
  display:none;
  z-index:40;
}

.sheet-backdrop.open{
  display:block;
}

.sheet{
  position:fixed;
  left:12px;
  right:12px;
  bottom:12px;
  width:calc(100vw - 24px);
  max-width:calc(100vw - 24px);
  overflow:hidden;
  z-index:41;
  background:var(--panel);
  border:1px solid var(--line);
  border-radius:22px;
  box-shadow:0 22px 48px rgba(15,23,42,.18);
  padding:14px;
  transform:translateY(calc(100% + 24px));
  transition:transform .22s ease;
}

.sheet.open{
  transform:translateY(0);
}

.sheet-header{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
  margin-bottom:12px;
}

.sheet-title{
  margin:0;
  font-size:16px;
  font-weight:800;
}

.sheet-close{
  width:36px;
  height:36px;
  border:none;
  border-radius:12px;
  background:#eef2fb;
  color:#4d5873;
  font-size:18px;
  cursor:pointer;
}

.sheet-body{
  display:grid;
  gap:12px;
  min-width:0;
  overflow-x:hidden;
}

.sheet-field{
  display:grid;
  gap:6px;
  min-width:0;
}

.sheet-field label{
  font-size:12px;
  font-weight:700;
  color:#334155;
}

.sheet-field input{
  width:100%;
  min-width:0;
  min-height:44px;
  border:1px solid var(--line);
  border-radius:14px;
  padding:0 12px;
  background:#fff;
}

.sheet-field input:disabled{
  color:#94a3b8;
  background:#f8fafc;
}

.sheet-field select{
  width:100%;
  min-width:0;
  max-width:100%;
  min-height:44px;
  border:1px solid var(--line);
  border-radius:14px;
  padding:0 12px;
  background:#fff;
  font:inherit;
}

.color-swatch-picker{
  display:flex;
  flex-wrap:nowrap;
  gap:5px;
  align-items:center;
  min-width:0;
}

.color-swatch{
  width:clamp(24px,7vw,32px);
  height:clamp(24px,7vw,32px);
  min-width:clamp(24px,7vw,32px);
  min-height:clamp(24px,7vw,32px);
  padding:0;
  border:2px solid rgba(148,163,184,.75);
  border-radius:999px;
  color:#111827;
  font:inherit;
  font-size:0;
  font-weight:700;
  cursor:pointer;
}

.color-swatch[data-color="#2a5bd7"],
.color-swatch[data-color="#2e7d32"],
.color-swatch[data-color="#8e44ad"],
.color-swatch[data-color="#8b5a2b"],
.color-swatch[data-color="#111827"]{
  color:#fff;
}

.color-swatch.is-selected{
  border-color:#111827;
  box-shadow:0 0 0 3px rgba(17,24,39,.22);
}

.sheet-hint{
  margin:0;
  font-size:12px;
  line-height:1.5;
  color:var(--muted);
  white-space:pre-line;
  overflow-wrap:anywhere;
  word-break:break-word;
}

.sheet-actions{
  display:grid;
  grid-template-columns:repeat(2,minmax(0,1fr));
  gap:10px;
  margin-top:4px;
}

.sheet-actions.has-move{
  grid-template-columns:repeat(3,minmax(0,1fr));
}

.sheet-actions .btn{
  min-width:0;
}

.action-primary{
  background:var(--accent);
  color:#fff;
  border-color:transparent;
}

.action-secondary{
  background:#e8f6ee;
  color:#166534;
  border-color:#b8dfc6;
}

.move-toolbar{
  position:fixed;
  left:12px;
  right:12px;
  bottom:var(--safe-bottom);
  z-index:45;
  display:none;
  grid-template-columns:1fr 1fr;
  gap:10px;
}

.move-toolbar.open{
  display:grid;
}

body.label-move-active .topbar,
body.label-move-active .bottom-bar{
  pointer-events:none;
  opacity:.42;
}

.stage-wrap.label-move-active .stage > *{
  filter:grayscale(1);
  opacity:.34;
  pointer-events:none;
}

.stage-wrap.label-move-active .stage .label-move-target,
.stage-wrap.label-move-active .stage .label-move-target *{
  filter:none;
  opacity:1;
  pointer-events:auto;
}

.stage-wrap.label-move-active .stage .label-move-target{
  cursor:grab;
}

.stage-wrap.label-move-active .stage .label-move-target:active{
  cursor:grabbing;
}

.save-grid{
  display:grid;
  gap:10px;
}

.save-grid .btn{
  width:100%;
}

@media (min-width: 900px){
  .app{
    max-width:900px;
    margin:0 auto;
    width:100%;
  }

  .topbar{
    grid-template-columns:112px 1fr 112px;
  }
}
