:root {
  --bg: #e4e0d8;
  --panel: #f7f5f1;
  --panel-line: #ddd8cf;
  --ink: #34322e;
  --muted: #7b766d;
  --active: #3d3a35;
  --active-ink: #fbfaf6;
  --hover: #ece8e1;
  --accent: #8a6f4d;
  --radius: 14px;
  --top: 56px;
  --left: 84px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Noto Sans CJK JP", sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#leinwand {
  position: fixed;
  inset: 0;
  touch-action: none;
  display: block;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Obere Leiste */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  min-height: 56px;
  padding: 5px 10px;
  padding-top: max(5px, env(safe-area-inset-top));
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--panel);
  border-bottom: 1px solid var(--panel-line);
  z-index: 2;
}
.spacer { flex: 1; }

.tb {
  min-width: 50px;
  height: 44px;
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  font-size: 10.5px;
}
.tb:hover, .tool:hover { background: var(--hover); }
.tb:disabled { opacity: 0.35; cursor: default; background: none; }
.tb.on { background: var(--hover); }

/* Einstellungen des aktiven Werkzeugs (obere Leiste) */
.ctx {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: 10px;
  padding-left: 10px;
  border-left: 1px solid var(--panel-line);
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.ctx::-webkit-scrollbar { display: none; }
.ctx [hidden] { display: none !important; }
.seg { display: flex; gap: 4px; flex: none; align-items: center; }
.seg-label { font-size: 11px; color: var(--muted); padding: 0 4px 0 2px; white-space: nowrap; }
.seg + .seg { padding-left: 8px; border-left: 1px solid var(--panel-line); }
.seg button {
  flex: none;
  height: 36px;
  min-width: 34px;
  padding: 0 9px;
  border-radius: 9px;
  font-size: 12.5px;
  font-weight: 600;
  border: 1.5px solid var(--panel-line);
  white-space: nowrap;
}
.seg.grades button { width: 34px; padding: 0; }
.seg.icons button {
  width: auto; min-width: 48px; padding: 0 5px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  font-size: 9.5px; font-weight: 500;
}
.seg.icons svg { width: 18px; height: 18px; }
.seg button.on { background: var(--active); color: var(--active-ink); border-color: var(--active); }
/* Abstand zwischen den H- und den B-Minen */
.grades button[data-g="HB"] { margin-left: 4px; margin-right: 4px; }

.swatch-btn {
  flex: none;
  width: 42px; height: 36px;
  border-radius: 12px;
  border: 1.5px solid var(--panel-line);
  display: flex; align-items: center; justify-content: center;
}
.swatch-btn.on { border-color: var(--active); }
.swatch-big {
  width: 26px; height: 26px; border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.15);
}

/* Farbwähler */
.panel {
  position: fixed;
  z-index: 4;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 18px;
  box-shadow: 0 12px 36px rgba(0,0,0,0.16);
}
.color-panel {
  top: calc(var(--top) + 8px);
  left: calc(var(--left) + 8px);
  width: 328px;
  max-width: calc(100vw - 24px);
  padding: 16px;
}
.sv {
  position: relative;
  height: 180px;
  border-radius: 12px;
  touch-action: none;
  cursor: crosshair;
}
.hue {
  position: relative;
  height: 32px;
  margin-top: 14px;
  border-radius: 16px;
  touch-action: none;
  cursor: ew-resize;
  background: linear-gradient(to right, #f00, #ff0, #0f0, #0ff, #00f, #f0f, #f00);
}
.knob {
  position: absolute;
  width: 26px; height: 26px;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.35), 0 2px 6px rgba(0,0,0,0.25);
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.hue .knob { top: 50%; }
.pal-title { font-size: 13px; color: var(--muted); margin: 14px 0 8px; }
.palette { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.palette button {
  aspect-ratio: 1;
  border-radius: 10px;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.15);
}
.palette button.on { outline: 3px solid var(--active); outline-offset: 2px; }

/* Werkzeugleiste links */
.toolbar {
  position: fixed;
  top: var(--top);
  left: 0;
  bottom: 0;
  width: var(--left);
  padding: 6px 6px max(6px, env(safe-area-inset-bottom));
  padding-left: max(6px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 3px;
  background: var(--panel);
  border-right: 1px solid var(--panel-line);
  z-index: 2;
  overflow-y: auto;
}

.tool {
  width: 100%;
  min-height: 46px;
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 10.5px;
  line-height: 1.1;
  text-align: center;
}
.tool svg { width: 21px; height: 21px; }
.tool.on { background: var(--active); color: var(--active-ink); }

.size {
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--panel-line);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-height: 140px;
}
.size-preview {
  width: 48px; height: 48px;
  display: flex; align-items: center; justify-content: center;
}
#size-dot { border-radius: 50%; background: var(--ink); min-width: 2px; min-height: 2px; }

.slider {
  position: relative;
  width: 44px;
  flex: 1;
  min-height: 120px;
  max-height: 320px;
  touch-action: none;
  cursor: ns-resize;
}
.slider .track {
  position: absolute;
  left: 50%; top: 14px; bottom: 14px;
  width: 6px;
  transform: translateX(-50%);
  border-radius: 3px;
  background: linear-gradient(to top, #d3cdc2, #8f877a);
}
.slider .thumb {
  position: absolute;
  left: 50%;
  width: 36px; height: 22px;
  transform: translate(-50%, -50%);
  border-radius: 14px;
  background: #fff;
  border: 1.5px solid #b9b1a4;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
}
.size-value { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }


/* Buntstift-Symbol: farbige Spitze */
.tool svg .tip { fill: var(--tool-color, #c0392b); stroke: none; }

.mask-badge {
  flex: none;
  padding: 8px 12px;
  border-radius: 10px;
  background: #f6dde0;
  color: #8e1f33;
  font-size: 13px;
  font-weight: 600;
}

/* Ebenen-Fenster */
.layers-panel {
  top: calc(var(--top) + 8px);
  right: 8px;
  bottom: 8px;
  width: 310px;
  max-width: calc(100vw - var(--left) - 24px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.lp-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px 8px; }
.lp-head h2 { margin: 0; font-size: 18px; }
.lp-add {
  width: 40px; height: 36px; border-radius: 10px;
  background: var(--active); color: var(--active-ink);
  display: flex; align-items: center; justify-content: center;
}
.layer-list { flex: 1; overflow-y: auto; padding: 0 10px; min-height: 120px; }
.layer-row {
  display: flex; align-items: center; gap: 8px;
  padding: 6px; margin-bottom: 6px;
  border-radius: 12px;
  border: 1.5px solid transparent;
  background: #fff;
  cursor: pointer;
}
.layer-row.on { border-color: var(--active); }
.layer-row.clip { margin-left: 22px; }
.layer-row .eye { width: 34px; height: 34px; border-radius: 10px; display: flex; align-items: center; justify-content: center; flex: none; }
.layer-row .eye.off { opacity: 0.35; }
.layer-row .name { flex: 1; font-size: 14px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-row .meta { font-size: 11.5px; color: var(--muted); display: block; }
.thumb {
  flex: none; width: 58px; height: 44px; border-radius: 6px;
  border: 1.5px solid var(--panel-line);
  background-color: #fff;
  background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%),
                    linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%);
  background-size: 10px 10px; background-position: 0 0, 5px 5px;
}
.thumb.sel { border-color: #c2324b; box-shadow: 0 0 0 1.5px #c2324b; }
.thumb.mask { background: #222; }
.lp-props { border-top: 1px solid var(--panel-line); padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.lp-row { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.lp-row > span:first-child { width: 72px; color: var(--muted); flex: none; }
.lp-row input[type=range] { flex: 1; height: 36px; accent-color: var(--active); }
.lp-row output { width: 42px; text-align: right; font-variant-numeric: tabular-nums; }
.lp-toggles, .lp-mask, .lp-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.lp-toggle { display: flex; align-items: center; gap: 8px; min-height: 34px; padding: 0 8px; border-radius: 10px; font-size: 13.5px; }
.lp-toggle .box { width: 22px; height: 22px; border-radius: 6px; border: 1.5px solid #b9b1a4; background: #fff; }
.lp-toggle.on .box { background: var(--active); border-color: var(--active); box-shadow: inset 0 0 0 3px #fff; }
.lp-btn {
  min-height: 34px; padding: 0 10px; border-radius: 9px;
  border: 1.5px solid var(--panel-line); background: #fff; font-size: 12.5px;
}
.lp-btn.icon { width: 36px; padding: 0; display: flex; align-items: center; justify-content: center; }
.lp-btn.on { background: #c2324b; border-color: #c2324b; color: #fff; }
.lp-btn.danger { color: #a3302a; }
.lp-btn:disabled { opacity: 0.35; }
.lp-btn[hidden] { display: none; }


/* Vektor: Füllung und Kontur */
.vsw {
  flex: none; height: 36px; min-width: 48px; padding: 0 5px;
  border-radius: 11px; border: 1.5px solid var(--panel-line);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 9.5px;
}
.vsw.on { border-color: var(--active); box-shadow: inset 0 0 0 1.5px var(--active); }
.vsw .sw { width: 20px; height: 14px; border-radius: 5px; box-shadow: inset 0 0 0 1px rgba(0,0,0,0.2); }
.vsw .sw.ring { background: #fff !important; border: 4px solid var(--ring, #222); box-shadow: none; }
.vsw .sw.none { background: #fff linear-gradient(to top right, transparent 45%, #d33 45%, #d33 55%, transparent 55%) !important; border-color: #ccc !important; }
.c-none {
  width: 100%; min-height: 34px; margin-bottom: 10px; border-radius: 9px;
  border: 1.5px solid var(--panel-line); background: #fff; font-size: 14px;
}
.c-none.on { border-color: var(--active); background: var(--hover); }
.t-text {
  width: 100%; min-height: 96px; font: inherit; font-size: 18px; padding: 10px 12px;
  border-radius: 12px; border: 1.5px solid var(--panel-line); margin-bottom: 14px; resize: vertical;
}


/* Speicher-Anzeige */
.save-state { font-size: 12px; color: var(--muted); white-space: nowrap; min-width: 0; }
.save-state.err { color: #a3302a; font-weight: 600; }

/* Teilen */
.share-panel { top: calc(var(--top) + 8px); right: 12px; width: 340px; max-width: calc(100vw - 24px); padding: 10px; display: flex; flex-direction: column; gap: 6px; }
.share-item {
  text-align: left; padding: 9px 12px; border-radius: 10px; background: #fff;
  border: 1.5px solid var(--panel-line); display: flex; flex-direction: column; gap: 2px;
}
.share-item b { font-size: 14px; }
.share-item span { font-size: 11.5px; color: var(--muted); }
.share-item:hover { border-color: var(--active); }

/* Galerie */
.gallery {
  position: fixed; inset: 0; z-index: 6; background: var(--bg);
  display: flex; flex-direction: column; overflow: hidden;
}
.g-head {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: max(14px, env(safe-area-inset-top)) 20px 14px;
  background: var(--panel); border-bottom: 1px solid var(--panel-line);
}
.g-head h1 { margin: 0; font-size: 22px; font-weight: 600; }
.g-new { min-height: 38px; padding: 0 18px; border-radius: 10px; font-size: 14px; }
.g-grid {
  flex: 1; overflow-y: auto; padding: 20px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 18px; align-content: start;
}
.g-card { background: var(--panel); border-radius: 16px; border: 1.5px solid var(--panel-line); overflow: hidden; display: flex; flex-direction: column; }
.g-card.current { border-color: var(--active); }
.g-thumb { aspect-ratio: 4 / 3; background: #fff center / contain no-repeat; cursor: pointer; border: 0; width: 100%; }
.g-info { padding: 10px 12px 4px; }
.g-name { font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.g-date { font-size: 12px; color: var(--muted); }
.g-actions { display: flex; gap: 6px; padding: 8px 10px 12px; }
.g-actions .lp-btn { flex: 1; }
.guide-link { display: inline-flex; align-items: center; text-decoration: none; color: var(--ink); font-weight: 600; }
.g-empty { text-align: center; color: var(--muted); font-size: 16px; }

/* Aquarell: nass-Anzeige */
.wc-wet { font-size: 12.5px; color: #2f6fa3; padding: 0 6px; white-space: nowrap; }

/* Stift-Info */
.infobox {
  position: fixed;
  right: 16px;
  top: calc(var(--top) + 16px);
  width: 300px;
  padding: 14px 16px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,0.1);
  z-index: 3;
  max-height: calc(100vh - var(--top) - 32px);
  overflow-y: auto;
}
.infobox h2 { margin: 0 0 10px; font-size: 17px; }
.infobox h3 { margin: 14px 0 2px; font-size: 14px; padding-top: 10px; border-top: 1px solid var(--panel-line); }
.set-block { margin-bottom: 12px; }
.set-label { display: flex; align-items: center; justify-content: space-between; font-size: 14px; margin-bottom: 4px; }
.set-range { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.set-block input[type=range] { flex: 1; width: 100%; height: 32px; accent-color: var(--active); }
.curve { width: 60px; height: 40px; background: #fff; border: 1px solid var(--panel-line); border-radius: 6px; }
.curve path { fill: none; stroke: var(--accent); stroke-width: 2.5; }
.infobox .hint { margin: 0 0 12px; font-size: 13px; color: var(--muted); }
.infobox dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; font-size: 15px; }
.infobox dt { color: var(--muted); }
.infobox dd { margin: 0; font-variant-numeric: tabular-nums; }
.bar { height: 6px; margin-top: 4px; background: var(--hover); border-radius: 3px; overflow: hidden; }
#i-pbar { height: 100%; width: 0; background: var(--accent); }

/* Dialog Neues Bild */
dialog {
  border: 0;
  border-radius: 20px;
  padding: 0;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  max-width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
}
dialog::backdrop { background: rgba(50,45,40,0.35); }
.neu { padding: 24px 28px; }
.neu h2 { margin: 0 0 16px; font-size: 22px; font-weight: 600; }
fieldset { border: 0; padding: 0; margin: 0 0 18px; }
legend { font-size: 14px; color: var(--muted); margin-bottom: 8px; }
.choices { display: flex; flex-wrap: wrap; gap: 8px; }
.choices button {
  min-height: 42px;
  padding: 6px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--panel-line);
  background: #fff;
  font-size: 15px;
  display: flex; align-items: center; gap: 10px;
}
.choices button.on { border-color: var(--active); box-shadow: inset 0 0 0 1.5px var(--active); }
.swatch { width: 24px; height: 24px; border-radius: 6px; border: 1px solid rgba(0,0,0,0.15); }
.custom { display: flex; align-items: center; gap: 10px; margin-top: 12px; font-size: 15px; flex-wrap: wrap; }
.custom input {
  width: 96px; height: 38px; margin-left: 6px;
  font: inherit; padding: 0 10px;
  border-radius: 10px; border: 1.5px solid var(--panel-line);
}
.warn { font-size: 14px; color: #8a4b2f; margin: 0 0 12px; }
.actions { display: flex; justify-content: flex-end; gap: 10px; }
.actions button { min-height: 42px; padding: 0 20px; border-radius: 10px; font-size: 15px; }
.primary { background: var(--active); color: var(--active-ink); }
.secondary { background: var(--hover); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  padding: 12px 20px;
  border-radius: 12px;
  background: rgba(52,50,46,0.92);
  color: #fff;
  font-size: 15px;
  z-index: 5;
  pointer-events: none;
}

/* Passen die Werkzeug-Einstellungen nicht in die Zeile (z. B. iPad hochkant),
   kommen sie in eine eigene Zeile darunter und dürfen umbrechen. */
.topbar.two-rows { flex-wrap: wrap; row-gap: 6px; }
.topbar.two-rows .ctx {
  order: 10; flex-basis: 100%;
  margin-left: 0; padding-left: 0; border-left: 0;
  flex-wrap: wrap; row-gap: 6px; overflow: visible;
}

@media (max-width: 900px) {
  .topbar { gap: 2px; padding-left: 8px; padding-right: 8px; }
  .ctx { gap: 6px; margin-left: 6px; padding-left: 6px; }
  .seg.grades button { width: 30px; height: 36px; font-size: 12px; border-radius: 8px; }
  .grades button[data-g="HB"] { margin-left: 2px; margin-right: 2px; }
  .seg.icons button { min-width: 44px; }
  .tb { min-width: 42px; font-size: 10px; }
}

/* Niedrige Bildschirme (iPad quer mit Safari-Leiste): Werkzeugleiste kompakter */
@media (max-height: 900px) {
  .tool { min-height: 42px; font-size: 10px; gap: 1px; }
  .tool svg { width: 20px; height: 20px; }
  .size { min-height: 120px; margin-top: 4px; padding-top: 8px; gap: 6px; }
  .size-preview { height: 32px; }
  .slider { min-height: 70px; }
}
@media (max-height: 780px) {
  .tool { min-height: 40px; }
  .size { min-height: 112px; }
  .slider { min-height: 56px; }
}
@media (max-height: 680px) {
  .tool { min-height: 36px; }
  .tool span { font-size: 10px; }
  .size-preview { display: none; }
}
