/* Plain, system-like look that follows the device's light/dark setting. */
:root {
  --bg: #f4f4f4;
  --surface: #fafafa;
  --surface-2: #ebebeb;
  --selected: #dcdcdc;
  --border: #dddddd;
  --text: #222222;
  --muted: #707070;
  --stage: #e6e6e6;
  --radius: 8px;
  --tap: 44px; /* minimum touch target */
  --toast-bottom: calc(176px + env(safe-area-inset-bottom));
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1c;
    --surface: #222222;
    --surface-2: #2e2e2e;
    --selected: #3d3d3d;
    --border: #333333;
    --text: #e4e4e4;
    --muted: #999999;
    --stage: #262626;
  }
}

*,
*::before,
*::after { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

/*
 * Layout
 * Portrait / narrow: header on top, image in the middle, tools along the bottom.
 * Landscape phones and wide screens: tools move into a side panel so the image
 * keeps as much height as possible (see the media query further down).
 */
body {
  margin: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas: "top" "stage" "tools";
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

button { font: inherit; color: inherit; }

svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}
svg .fill { fill: currentColor; stroke: none; }

/* ---- Header ---- */
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.actions { display: flex; flex: none; gap: 6px; }
.actions.end { margin-left: auto; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: var(--tap);
  min-height: var(--tap);
  padding: 0 12px;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface-2);
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:hover:not(:disabled) { background: var(--selected); }
.btn.primary { background: var(--selected); }
.btn.ghost { background: transparent; }
.btn.large { min-height: 48px; padding: 0 20px; font-size: 16px; }
.btn:disabled, .icon-btn:disabled { opacity: 0.4; cursor: default; }
:is(.btn, .icon-btn, .tool, .swatch, summary):focus-visible { outline: 2px solid var(--muted); outline-offset: 2px; }

/* Phones: header buttons are icon-only. */
@media (max-width: 560px) {
  .topbar .btn { padding: 0; }
  .topbar .btn-label { display: none; }
}

/* ---- Image area ---- */
.stage {
  grid-area: stage;
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--stage);
}

#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  cursor: crosshair;
}
#canvas[data-tool="move"] { cursor: grab; }

/* Start screen: scrolls when it doesn't fit; centered (via margin:auto) when it does. */
.empty {
  position: absolute;
  inset: 0;
  display: flex;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 24px max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: var(--bg);
}
.empty-inner {
  margin: auto;
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  text-align: center;
}
.empty p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.drop-overlay {
  position: absolute;
  inset: 12px;
  display: grid;
  place-items: center;
  border: 2px dashed var(--muted);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--muted);
  pointer-events: none;
}

/* ---- Tools ---- */
.toolbar {
  grid-area: tools;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
  padding: 8px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
/* On tablets in portrait, keep the controls a comfortable width instead of stretching. */
.toolbar > * { width: 100%; max-width: 640px; margin-inline: auto; }


.options { display: flex; align-items: center; gap: 8px; min-width: 0; }
.row { display: flex; align-items: center; gap: 6px; min-width: 0; }
.size { flex: 1; color: var(--muted); font-size: 14px; }
.size input { flex: 1; min-width: 60px; height: var(--tap); margin: 0; accent-color: var(--muted); }
.history { display: flex; flex: none; gap: 4px; margin-left: auto; }

.icon-btn {
  display: grid;
  place-items: center;
  width: var(--tap);
  height: var(--tap);
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface-2);
  cursor: pointer;
  touch-action: manipulation;
}

.swatch {
  display: grid;
  place-items: center;
  width: var(--tap);
  height: var(--tap);
  padding: 0;
  border: 2px solid transparent;
  border-radius: 50%;
  background: none;
  cursor: pointer;
}
.swatch[aria-checked="true"] { border-color: var(--muted); }
.chip { width: 28px; height: 28px; border-radius: 50%; border: 1px solid var(--border); }
.chip.black { background: #000; }
.chip.white { background: #fff; }

.tools { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 0;
  min-height: 52px;
  padding: 6px 2px;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: 12px;
  cursor: pointer;
  touch-action: manipulation;
}
.tool span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool { color: var(--muted); }
.tool[aria-checked="true"] { background: var(--selected); color: var(--text); }

/* Short portrait screens (e.g. iPhone SE): tighter toolbar, more room for the image. */
@media (orientation: portrait) and (max-height: 700px) {
  .toolbar { gap: 6px; padding-top: 6px; }
  .tool { min-height: 46px; padding: 4px 2px; }
}

/*
 * Side panel: phones in landscape and wide screens.
 */
@media (orientation: landscape) and (max-height: 600px), (min-width: 1000px) {
  :root { --toast-bottom: calc(20px + env(safe-area-inset-bottom)); }

  body {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "top top" "stage tools";
  }

  .toolbar {
    width: 260px;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 12px;
    padding-right: max(12px, env(safe-area-inset-right));
    padding-bottom: max(12px, env(safe-area-inset-bottom));
    border-top: 0;
    border-left: 1px solid var(--border);
  }
  .toolbar > * { max-width: none; margin-inline: 0; }

  .tools { order: 1; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .options { order: 2; flex-wrap: wrap; }
  .size { flex-basis: 100%; }
  .history { flex-basis: 100%; margin-left: 0; }
  .history .icon-btn { flex: 1; }
}

/* Landscape phones: compact header and a narrower panel. */
@media (orientation: landscape) and (max-height: 600px) {
  .topbar { padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: 4px; }
  .topbar .btn { min-height: 38px; }
  .toolbar { width: 188px; gap: 6px; padding: 8px; padding-right: max(8px, env(safe-area-inset-right)); }
  .tool { min-height: 44px; padding: 4px 2px; }
  .stage { margin-left: env(safe-area-inset-left); }
}

/* ---- Export sheet ---- */
.sheet {
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.4); }
.sheet h2 { margin: 0 0 12px; font-size: 17px; font-weight: 600; }
.sheet p { margin: 0 0 16px; font-size: 14px; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; }
.sheet .note { margin: -2px 0 6px; color: var(--muted); font-size: 13px; }

/* ---- Toast ---- */
.toast {
  position: fixed;
  left: 50%;
  bottom: var(--toast-bottom);
  z-index: 10;
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 14px;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, 8px);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---- Privacy shield: no transitions, it must cover the image on the very next frame ---- */
.shield {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  background: var(--bg);
}
html.shielded .shield { display: block; }
html.shielded #canvas,
html.shielded .sheet { visibility: hidden; }
