/* =========================================================
   粋の間 — iki no ma
   Esthétique reprise de ji no ma : le papier domine,
   hiérarchie par l'espacement, aucune couleur qui ne soit
   fonctionnelle. Le motif au centre, les contrôles s'effacent.
   ========================================================= */

:root {
  --papier:        #f6f3ec;  /* fond général */
  --fond-panneau:  #fbf9f4;  /* panneau de contrôles */
  --encre:         #161310;  /* texte, éléments actifs */
  --discret:       #8b8378;  /* texte secondaire */
  --trait:         rgba(22, 19, 16, 0.12);
  --trait-fort:    rgba(22, 19, 16, 0.28);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  background: var(--papier);
  color: var(--encre);
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "Songti SC", Georgia, serif;
  font-weight: 400;
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.app { display: flex; height: 100vh; }

/* ---------------- Panneau (à droite, comme ji no ma) ---------------- */
.panel {
  order: 2;               /* le motif d'abord, les contrôles ensuite */
  width: 330px;
  min-width: 330px;
  background: var(--fond-panneau);
  border-left: 1px solid var(--trait);
  display: flex;
  flex-direction: column;
}

/* Titre : kanji puis romaji, à la manière de ji no ma */
.brand {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  padding: 2rem 1.8rem 1.2rem;
}
.brand-line {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.brand-kanji {
  font-size: 1.5rem;
  letter-spacing: 0.15em;
  line-height: 1;
  color: var(--encre);
}
.brand-name {
  font-size: 0.7rem;
  letter-spacing: 0.35em;
  color: var(--discret);
  text-transform: lowercase;
}

/* Sélecteur de langue : les trois choix restent visibles — un menu fermé
   ne laisserait pas deviner qu'il y a d'autres langues. */
.lang {
  margin-left: auto;
  display: flex;
  gap: 1px;
  background: var(--trait);
  border: 1px solid var(--trait);
}
.lang-opt {
  appearance: none;
  border: none;
  background: var(--fond-panneau);
  color: var(--discret);
  font-family: inherit;
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  line-height: 1;
  padding: 0.28rem 0.42rem;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.lang-opt:hover { color: var(--encre); }
.lang-opt.actif { background: var(--encre); color: var(--papier); }

/* Barre d'outils */
.toolbar {
  display: flex;
  gap: 1px;
  margin: 0 1.8rem 1.4rem;
  background: var(--trait);
  border: 1px solid var(--trait);
}
.tool {
  flex: 1;
  appearance: none;
  background: var(--fond-panneau);
  border: none;
  color: var(--discret);
  font-family: inherit;
  font-size: 15px;
  line-height: 1;
  padding: 0.5rem 0;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.tool:hover { background: var(--encre); color: var(--papier); }
.tool.on { background: var(--encre); color: var(--papier); }
.tool:disabled { color: var(--trait-fort); background: var(--fond-panneau); cursor: default; }

/* Corps scrollable */
.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 0 1.8rem 2rem;
}
.panel-body::-webkit-scrollbar { width: 8px; }
.panel-body::-webkit-scrollbar-thumb { background: var(--trait); }

.sec { border-top: 1px solid var(--trait); padding: 1.4rem 0 0.2rem; }
.sec:first-child { border-top: none; padding-top: 0; }

.sec-head {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin-bottom: 1.1rem;
}
.sec-num {
  font-size: 0.62rem;
  color: var(--discret);
  font-variant-numeric: tabular-nums;
}
.sec-title {
  font-size: 0.66rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--discret);
}
.lock-sec { margin-left: auto; }

/* Verrous : l'état se lit au glyphe (○ ◐ ●), pas à la couleur */
.lock {
  appearance: none;
  background: none;
  border: none;
  color: var(--trait-fort);
  font-size: 0.85rem;
  line-height: 1;
  width: 16px;
  height: 16px;
  padding: 0;
  cursor: pointer;
  flex-shrink: 0;
  transition: color .15s;
  font-family: inherit;
}
.lock:hover { color: var(--discret); }
.lock.locked, .lock.partial { color: var(--encre); }

/* Contrôles */
.ctrl {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem 0.5rem;
  margin-bottom: 1.1rem;
}
.ctrl > label {
  font-size: 0.85rem;
  letter-spacing: 0.02em;
  color: var(--encre);
  flex: 1;
  min-width: 0;
}
.ctrl label.check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}
.ctrl select { flex-basis: 100%; order: 5; }

select, .btn-line, input[type="text"], textarea {
  appearance: none;
  background: var(--fond-panneau);
  border: 1px solid var(--trait);
  color: var(--encre);
  padding: 0.45rem 0.55rem;
  font-size: 0.82rem;
  font-family: inherit;
  outline: none;
  transition: border-color .15s;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--discret) 50%),
                    linear-gradient(135deg, var(--discret) 50%, transparent 50%);
  background-position: calc(100% - 14px) 52%, calc(100% - 9px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 26px;
}
select:hover, select:focus,
input[type="text"]:focus, textarea:focus { border-color: var(--trait-fort); }

.btn-line {
  cursor: pointer;
  text-align: center;
  width: 100%;
  margin-top: 0.2rem;
}
.btn-line:hover { background: var(--encre); color: var(--papier); border-color: var(--encre); }
.btn-line:disabled { color: var(--trait-fort); background: var(--fond-panneau); border-color: var(--trait); cursor: default; }
.btn-line.ok { background: var(--encre); color: var(--papier); border-color: var(--encre); }

/* Glissières sobres, sans habillage superflu */
.range-row {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  flex-basis: 100%;
  order: 5;
}
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  height: 1px;
  background: var(--trait-fort);
  outline: none;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 11px; height: 11px;
  border-radius: 50%;
  background: var(--encre);
  border: none;
  cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
  width: 11px; height: 11px;
  border-radius: 50%;
  background: var(--encre);
  border: none;
  cursor: pointer;
}
.rv {
  font-family: "SFMono-Regular", ui-monospace, Menlo, monospace;
  font-size: 0.72rem;
  color: var(--discret);
  font-variant-numeric: tabular-nums;
  min-width: 38px;
  text-align: right;
}

/* Couleurs : seule couleur admise, car strictement fonctionnelle */
.color-row { display: flex; align-items: center; gap: 0.55rem; }
input[type="color"] {
  -webkit-appearance: none;
  appearance: none;
  width: 30px; height: 22px;
  border: 1px solid var(--trait);
  background: none;
  cursor: pointer;
  padding: 0;
}
input[type="color"]::-webkit-color-swatch-wrapper { padding: 2px; }
input[type="color"]::-webkit-color-swatch { border: none; }
.hex {
  font-family: "SFMono-Regular", ui-monospace, Menlo, monospace;
  font-size: 0.72rem;
  color: var(--discret);
}

input[type="checkbox"] {
  -webkit-appearance: none;
  appearance: none;
  width: 14px; height: 14px;
  border: 1px solid var(--trait-fort);
  background: var(--fond-panneau);
  cursor: pointer;
  position: relative;
  flex-shrink: 0;
}
input[type="checkbox"]:checked { background: var(--encre); border-color: var(--encre); }
input[type="checkbox"]:checked::after {
  content: "✓";
  position: absolute;
  top: -3px; left: 1px;
  font-size: 11px;
  color: var(--papier);
}

/* Palettes : menu déroulant — au repos, seule la palette en cours se voit.
   Le nuancier parle, pas le libellé. */
.pal-select { position: relative; }

.pal-tete {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  appearance: none;
  background: var(--fond-panneau);
  border: 1px solid var(--trait);
  padding: 0.35rem 0.4rem;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  transition: border-color .15s;
}
.pal-tete:hover { border-color: var(--trait-fort); }
.pal-chev { margin-left: auto; font-size: 0.6rem; color: var(--discret); line-height: 1; }

.pal-menu {
  display: none;
  position: absolute;
  left: 0; right: 0;
  top: calc(100% + 2px);
  z-index: 20;
  background: var(--fond-panneau);
  border: 1px solid var(--trait-fort);
  max-height: 264px;
  overflow-y: auto;
}
.pal-select.ouvert .pal-menu { display: block; }
.pal-menu::-webkit-scrollbar { width: 8px; }
.pal-menu::-webkit-scrollbar-thumb { background: var(--trait); }

.pal {
  width: 100%;
  appearance: none;
  border: none;
  border-bottom: 1px solid var(--trait);
  background: var(--fond-panneau);
  cursor: pointer;
  padding: 0.35rem 0.4rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-family: inherit;
  text-align: left;
  transition: background .15s;
}
.pal:last-child { border-bottom: none; }
.pal:hover { background: var(--papier); }

.pal-bande { display: flex; height: 13px; width: 56px; flex-shrink: 0; border: 1px solid var(--trait); }
.pal-bande i { flex: 1; display: block; }
.pal-nom { font-size: 0.66rem; letter-spacing: 0.06em; color: var(--discret); }

.pal.actif { background: var(--encre); }
.pal.actif .pal-nom { color: var(--papier); }
.pal.actif .pal-bande { border-color: var(--encre); }

/* Séquenceur : 8 pas, plein = trait tracé, vide = interstice */
.seq {
  display: flex;
  gap: 1px;
  flex-basis: 100%;
  order: 5;
  background: var(--trait);
  border: 1px solid var(--trait);
}
.seq-step {
  flex: 1;
  appearance: none;
  border: none;
  background: var(--fond-panneau);
  height: 20px;
  padding: 0;
  cursor: pointer;
  transition: background .15s;
}
.seq-step:hover { background: var(--trait); }
.seq-step.on { background: var(--encre); }
.seq-step.on:hover { background: var(--trait-fort); }

/* Zone de dépôt */
.drop {
  border: 1px dashed var(--trait-fort);
  padding: 1.4rem 0.8rem;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.drop:hover, .drop.over { border-color: var(--encre); background: var(--papier); }
.drop-label { font-size: 0.85rem; color: var(--encre); }
.drop-hint  { font-size: 0.62rem; color: var(--discret); letter-spacing: 0.1em; }
.img-note {
  font-size: 0.68rem;
  color: var(--discret);
  margin: 0.6rem 0 1rem;
  line-height: 1.45;
}

/* Presets */
.preset-add { display: flex; gap: 0.4rem; margin-bottom: 0.7rem; }
.preset-add input { flex: 1; }
.preset-add .btn-line { width: auto; margin-top: 0; white-space: nowrap; }
.preset-list { display: flex; flex-direction: column; gap: 1px; background: var(--trait); border: 1px solid var(--trait); }
.preset-list:empty { border: none; }
.preset-item {
  display: flex; align-items: center; gap: 0.3rem;
  background: var(--fond-panneau);
  padding: 0.3rem 0.3rem 0.3rem 0.6rem;
}
.preset-item .pname {
  flex: 1; font-size: 0.82rem; cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.preset-item .pname:hover { text-decoration: underline; text-underline-offset: 3px; }
.preset-item button {
  background: none; border: none; color: var(--discret);
  cursor: pointer; font-size: 0.72rem; padding: 0.25rem 0.4rem; font-family: inherit;
}
.preset-item button:hover { color: var(--encre); }
.preset-empty { font-size: 0.72rem; color: var(--discret); font-style: italic; }

.export-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.4rem; }
.export-grid .btn-line { margin-top: 0; }

/* Champ caractères / texte + pré-sélection */
.char-input {
  flex-basis: 100%;
  order: 4;
  width: 100%;
  min-height: 32px;
  resize: vertical;
  text-align: left;
  font-size: 15px;
  line-height: 1.5;
}

/* Ligne police + bouton d'import */
.font-row { display: flex; gap: 0.4rem; flex-basis: 100%; order: 5; }
.font-row select { flex: 1; }
.font-up {
  appearance: none;
  background: var(--fond-panneau);
  border: 1px solid var(--trait);
  color: var(--discret);
  width: 30px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
  flex-shrink: 0;
  transition: background .15s, color .15s, border-color .15s;
}
.font-up:hover { background: var(--encre); color: var(--papier); border-color: var(--encre); }

.char-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  flex-basis: 100%;
  order: 6;
  background: var(--trait);
  border: 1px solid var(--trait);
}
.char-chip {
  appearance: none;
  background: var(--fond-panneau);
  border: none;
  color: var(--encre);
  width: 26px; height: 26px;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  font-family: inherit;
  transition: background .15s, color .15s;
}
.char-chip:hover { background: var(--encre); color: var(--papier); }

.panel-footer {
  padding: 0.9rem 1.8rem;
  border-top: 1px solid var(--trait);
  font-size: 0.62rem;
  color: var(--discret);
  letter-spacing: 0.16em;
}

/* ---------------- Aperçu : le motif occupe la place centrale ---------------- */
.preview {
  order: 1;
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--papier);
}
.canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  overflow: hidden;
  min-height: 0;
}
#cvs {
  max-width: 100%;
  max-height: 100%;
  border: 1px solid var(--trait);
  box-shadow: 0 2px 28px rgba(22, 19, 16, 0.07);
}

/* Galerie */
.gallery {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0.5rem;
  padding: 0.9rem 1.1rem;
  background: var(--fond-panneau);
  border-top: 1px solid var(--trait);
  max-height: 34vh;
  overflow-y: auto;
}
.gallery.hidden { display: none; }
.gallery canvas {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: contain;
  background: var(--papier);
  border: 1px solid var(--trait);
  cursor: pointer;
  transition: border-color .15s, transform .15s;
}
.gallery canvas:hover { border-color: var(--encre); transform: translateY(-2px); }

/* Toast */
.toast {
  position: absolute;
  bottom: 1.6rem;
  left: 50%;
  transform: translateX(-50%) translateY(0.6rem);
  background: var(--encre);
  color: var(--papier);
  font-size: 0.78rem;
  letter-spacing: 0.03em;
  padding: 0.5rem 1rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s, transform .3s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 820px) {
  .app { flex-direction: column; overflow: auto; height: auto; }
  body { overflow: auto; }
  .panel { width: 100%; min-width: 0; border-left: none; border-top: 1px solid var(--trait); }
  .gallery { grid-template-columns: repeat(4, 1fr); }
}
