:root {
  --sea: #1d5f8f;
  --sea-dark: #144566;
  --sun: #f2b632;
  --ink: #1b1b1b;
  --muted: #5b6470;
  --paper: #ffffff;
  --bg: #f4f1ea;
  --line: #d9d4c7;
  --radius: 14px;
  --tap: 44px;
  font-family: "Nunito", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; }

.app {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-areas: "top" "motifs" "stage" "side";
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Header */
.top {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--paper);
  border-bottom: 3px solid var(--ink);
}
.brand { display: block; flex: none; line-height: 0; }
.brand img { display: block; height: 36px; width: auto; }
@media (max-width: 560px) { .brand img { height: 28px; } }
@media (max-width: 360px) { .brand img { height: 24px; } }
/* Where am I: links to the start page and the town page, above the title */
.where { flex: 1; min-width: 0; }
.crumbs { display: flex; align-items: center; gap: 6px; font-size: .85rem; font-weight: 800; white-space: nowrap; }
.crumbs a { color: var(--sea); text-decoration: none; padding: 2px 0; }
.crumbs a:hover { text-decoration: underline; }
.crumbs span { color: var(--muted); }
.title {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lang { margin-left: auto; display: flex; border: 2px solid var(--ink); border-radius: 999px; overflow: hidden; flex: none; }
.lang button {
  min-width: var(--tap); min-height: 44px;
  border: 0; background: transparent; font-weight: 800; cursor: pointer;
}
.lang button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }

.panel-label {
  margin: 0 0 8px;
  font-size: .8rem;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
}

/* Motif picker */
.motifs {
  grid-area: motifs;
  padding: 8px 16px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.motifs .panel-label { display: none; }
#motif-list {
  display: flex;
  gap: 8px;
  margin: 0; padding: 2px;
  list-style: none;
  overflow-x: auto;
  scrollbar-width: none;
}
#motif-list::-webkit-scrollbar { display: none; }
.motif {
  flex: none;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  width: 88px;
  padding: 4px;
  border: 2px solid transparent;
  border-radius: 10px;
  background: none;
  cursor: pointer;
}
.motif img {
  display: block;
  width: 52px;
  aspect-ratio: 784 / 1094;
  object-fit: cover;
  border: 2px solid var(--ink);
  border-radius: 6px;
  background: var(--paper);
}
.motif span { max-width: 100%; font-size: .68rem; font-weight: 700; line-height: 1.15; text-align: center; overflow-wrap: anywhere; hyphens: auto; }
.motif[aria-current="true"] { border-color: var(--sea); background: #e6f0f7; }
.motif:focus-visible, .swatch:focus-visible, .tool:focus-visible, .lang button:focus-visible {
  outline: 3px solid var(--sun); outline-offset: 2px;
}

/* Canvas */
.stage {
  grid-area: stage;
  position: relative;
  min-height: 0;
  margin: 8px;
}
#canvas {
  position: absolute;
  inset: 0;
  margin: auto;
  max-width: 100%;
  max-height: 100%;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: 0 6px 0 rgba(0,0,0,.08);
  touch-action: none;
  cursor: crosshair;
  image-rendering: auto;
}
.status {
  position: absolute; left: 50%; bottom: 12px;
  transform: translateX(-50%);
  margin: 0; padding: 6px 12px;
  border-radius: 999px;
  background: var(--ink); color: var(--paper);
  font-size: .85rem; font-weight: 700;
  pointer-events: none;
  opacity: 0; transition: opacity .25s;
}
.status.show { opacity: .9; }

/* Palette + tools */
.side {
  grid-area: side;
  padding: 8px 16px 10px;
  background: var(--paper);
  border-top: 3px solid var(--ink);
}
.side > .panel-label, .hint, .promo { display: none; }

.palette {
  display: flex;
  gap: 8px;
  padding: 4px 2px 8px;
  overflow-x: auto;
  scrollbar-width: thin;
}
.swatch {
  flex: none;
  position: relative;
  width: var(--tap); height: var(--tap);
  border: 2.5px solid var(--ink);
  border-radius: 50%;
  background: var(--c);
  cursor: pointer;
  padding: 0;
}
.swatch[aria-checked="true"] {
  box-shadow: 0 0 0 3px var(--paper), 0 0 0 6px var(--ink);
}
.swatch svg { width: 22px; height: 22px; fill: none; stroke: var(--ink); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.swatch.eraser { display: grid; place-items: center; background: var(--paper); }
.swatch.custom {
  display: grid; place-items: center;
  background: conic-gradient(#e53935, #fdd835, #43a047, #1e88e5, #ab47bc, #e53935);
}
.swatch.custom .dot {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--ink); background: var(--c, #fff);
}
.swatch.custom input {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  opacity: 0; cursor: pointer; border: 0; padding: 0;
}

.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tool {
  min-height: var(--tap);
  display: flex; align-items: center; justify-content: center; gap: 6px;
  padding: 4px 6px;
  border: 2px solid var(--ink);
  border-radius: 12px;
  background: var(--paper);
  font-size: .85rem; font-weight: 700;
  cursor: pointer;
}
.tool svg { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.tool:hover:not(:disabled) { background: #fff6dd; }
.tool:active:not(:disabled) { transform: translateY(1px); }
.tool:disabled { opacity: .4; cursor: default; }
#download { background: var(--sea); border-color: var(--sea-dark); color: var(--paper); }
#download:hover { background: var(--sea-dark); }

@media (max-width: 380px) {
  .tool { flex-direction: column; gap: 2px; font-size: .72rem; }
}
/* Phones: the links replace the title (kept for screen readers), and are big enough to tap */
@media (max-width: 899px) {
  .title { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .crumbs { font-size: .95rem; }
  .crumbs a { display: inline-flex; align-items: center; min-height: 44px; overflow: hidden; text-overflow: ellipsis; }
}
/* Narrow phones: the links get their own row under the logo and the language buttons */
@media (max-width: 559px) {
  .top { flex-wrap: wrap; row-gap: 0; padding-bottom: 0; }
  .where { order: 3; flex: 1 0 100%; margin-top: -2px; }
}

/* Wide screens: picker left, canvas centre, palette right */
@media (min-width: 900px) {
  .app {
    grid-template-areas: "top top top" "motifs stage side";
    grid-template-columns: 200px minmax(0, 1fr) 280px;
    grid-template-rows: auto minmax(0, 1fr);
  }
  .title { font-size: 1.1rem; }
  .motifs {
    border-bottom: 0;
    border-right: 3px solid var(--ink);
    overflow-y: auto;
    padding: 16px;
  }
  .motifs .panel-label { display: block; }
  #motif-list { flex-direction: column; overflow: visible; }
  .motif { width: 100%; flex-direction: row; gap: 10px; text-align: left; }
  .motif img { width: 56px; }
  .motif span { font-size: .9rem; text-align: left; }
  .stage { margin: 20px; }
  .side {
    border-top: 0;
    border-left: 3px solid var(--ink);
    padding: 16px;
    overflow-y: auto;
    display: flex; flex-direction: column; gap: 16px;
  }
  .side > .panel-label { display: block; margin-bottom: -8px; }
  .palette { flex-wrap: wrap; overflow: visible; padding: 4px; gap: 10px; }
  .tools { grid-template-columns: 1fr 1fr; }
  .hint { display: block; margin: 0; color: var(--muted); font-size: .9rem; }
  .promo {
    display: block; margin: auto 0 0; padding: 12px;
    border: 2px dashed var(--line); border-radius: 12px;
    font-size: .9rem; line-height: 1.4;
  }
  .promo a { display: inline-block; margin-top: 4px; color: var(--sea); font-weight: 800; }
}

/* Print only the picture */
#print-area { display: none; }
@media print {
  @page { size: A4 portrait; margin: 12mm; }
  body { background: #fff; }
  .app { display: none; }
  #print-area { display: block; }
  #print-img { display: block; width: 100%; max-height: 270mm; object-fit: contain; }
}
