@font-face { font-family: "Brand"; src: url("../fonts/CinzelDecorative-Bold.ttf"); font-display: swap; }

:root {
    --ink: #1b1712;
    --ink-2: #4a4237;
    --muted: #857a6b;
    --paper: #f5efe3;
    --card: #fffdf8;
    --line: #e3d9c6;
    --accent: #7a1b26;
    --accent-2: #5e1219;
    --gold: #b88a3e;
    --ok: #2f6b3c;
    --radius: 14px;
    --tap: 52px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
    font: 17px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    color: var(--ink);
    background: var(--paper);
    min-height: 100vh;
    padding-bottom: env(safe-area-inset-bottom);
    -webkit-user-select: none;
    user-select: none;
}
input, textarea { -webkit-user-select: text; user-select: text; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }
.muted { color: var(--muted); }

/* ---------- Top bar ---------- */
.topbar {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; justify-content: space-between; gap: 16px;
    padding: calc(10px + env(safe-area-inset-top)) 20px 10px;
    background: var(--ink); color: var(--paper);
    border-bottom: 3px solid var(--gold);
}
.topbar .brand { font-family: "Brand", Georgia, serif; font-size: 22px; color: var(--paper); text-decoration: none; letter-spacing: .02em; }
.topbar nav { display: flex; gap: 4px; flex-wrap: wrap; }
.topbar nav a {
    display: inline-flex; align-items: center; gap: 8px;
    min-height: 44px; padding: 0 16px; border-radius: 10px;
    color: #d9cfbd; text-decoration: none; font-weight: 600;
}
.topbar nav a.on { background: #2e2820; color: #fff; }
.topbar nav a.muted { color: #8f8574; font-weight: 500; }
.badge {
    min-width: 26px; height: 26px; padding: 0 7px; border-radius: 13px;
    display: inline-flex; align-items: center; justify-content: center;
    background: var(--gold); color: var(--ink); font-size: 14px; font-weight: 700;
}

/* ---------- Login ---------- */
body.login { display: flex; align-items: center; justify-content: center; background: var(--ink); color: var(--paper); }
.pin-wrap { text-align: center; padding: 24px; }
.pin-wrap .brand { font-family: "Brand", Georgia, serif; font-size: 40px; margin: 0 0 6px; color: var(--paper); }
.pin-help { color: #b5aa96; margin: 0 0 22px; }
.pin-dots { display: flex; gap: 18px; justify-content: center; margin-bottom: 8px; }
.pin-dots span { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--gold); transition: background .12s; }
.pin-dots span.on { background: var(--gold); }
.pin-dots.shake { animation: shake .35s; }
@keyframes shake { 25% { transform: translateX(-10px); } 75% { transform: translateX(10px); } }
.pin-error { min-height: 24px; color: #f09a9a; margin: 6px 0 14px; }
.keypad { display: grid; grid-template-columns: repeat(3, 84px); gap: 16px; justify-content: center; }
.key {
    width: 84px; height: 84px; border-radius: 50%;
    border: 1px solid #3a3329; background: #26211a; color: var(--paper);
    font-size: 30px; font-weight: 500;
}
.key:active { background: var(--gold); color: var(--ink); }
.key-del { background: transparent; border-color: transparent; font-size: 26px; }

/* ---------- Designer ---------- */
.designer {
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(320px, 1fr);
    gap: 22px; padding: 22px; max-width: 1400px; margin: 0 auto;
}
@media (max-width: 900px) and (orientation: portrait) {
    .designer { grid-template-columns: 1fr; }
    .stage { position: static !important; }
}
.stage { position: sticky; top: 92px; align-self: start; }
.mug-frame {
    position: relative; width: 100%;
    background: #fff; border-radius: var(--radius);
    box-shadow: 0 1px 0 var(--line), 0 12px 30px rgba(60, 40, 10, .12);
    outline: 2px dashed var(--line); outline-offset: -10px;
    overflow: hidden;
}
.mug-frame img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; opacity: 0; transition: opacity .15s; }
.mug-frame img.ready { opacity: 1; }
.mug-frame img.loading { opacity: .55; }
.preview-empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 18px; }
.stage-note { text-align: center; color: var(--muted); font-size: 15px; margin: 12px 0 0; }

.controls { display: flex; flex-direction: column; gap: 20px; }
.field-label { display: flex; justify-content: space-between; align-items: baseline; font-weight: 700; font-size: 15px; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-2); margin-bottom: 10px; }
.field-label .hint { text-transform: none; letter-spacing: 0; font-weight: 500; color: var(--muted); }
.counter { font-weight: 600; color: var(--muted); letter-spacing: 0; }
.counter.full { color: var(--accent); }

input[type=text] {
    width: 100%; min-height: var(--tap); padding: 0 16px;
    font-size: 20px; /* 16px+ prevents iOS zoom */
    border: 2px solid var(--line); border-radius: 12px; background: var(--card); color: var(--ink);
    outline: none; -webkit-appearance: none;
}
input[type=text]:focus { border-color: var(--gold); }

.graphic-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 10px; max-height: 330px; overflow-y: auto; padding: 2px; -webkit-overflow-scrolling: touch; }
.graphic-tile {
    display: flex; flex-direction: column; align-items: center; gap: 6px;
    padding: 10px 6px 8px; border-radius: 12px;
    border: 2px solid var(--line); background: var(--card);
}
.graphic-tile img { width: 100%; height: 72px; object-fit: contain; pointer-events: none; }
.graphic-tile span { font-size: 13px; color: var(--ink-2); line-height: 1.2; text-align: center; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.graphic-tile.on { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(122, 27, 38, .18); }

.segmented { display: grid; grid-template-columns: 1fr 1fr; background: #ebe2d0; border-radius: 12px; padding: 4px; gap: 4px; }
.segmented button { min-height: 48px; border: 0; border-radius: 9px; background: transparent; font-weight: 600; color: var(--ink-2); }
.segmented button.on { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

.chips { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
.chips button { min-height: var(--tap); border-radius: 12px; border: 2px solid var(--line); background: var(--card); font-size: 22px; }
.chips button.on { border-color: var(--accent); background: #fbf2ee; }

.swatches { display: flex; flex-wrap: wrap; gap: 12px; }
.swatches button {
    width: var(--tap); height: var(--tap); border-radius: 50%;
    border: 3px solid var(--card); background: var(--sw);
    box-shadow: 0 0 0 2px var(--line);
}
.swatches button span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.swatches button.on { box-shadow: 0 0 0 3px var(--accent); }

.btn-primary, .btn-secondary {
    display: inline-flex; align-items: center; justify-content: center;
    min-height: 58px; padding: 0 28px; border-radius: 14px;
    font-size: 19px; font-weight: 700; text-decoration: none; border: 0;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:active { background: var(--accent-2); }
.btn-primary:disabled { background: #c8bba6; color: #f4efe6; }
.btn-secondary { background: var(--card); color: var(--ink); border: 2px solid var(--line); }
.add-btn { width: 100%; }
.add-stage { margin-top: 14px; }
.btn-small { min-height: 44px; padding: 0 14px; border-radius: 10px; border: 2px solid var(--line); background: var(--card); font-weight: 600; }
.btn-link { background: none; border: 0; padding: 8px 4px; min-height: 44px; color: var(--accent); font-weight: 600; }
.btn-link.danger { color: #9a2a2a; }
.empty-note { color: var(--muted); margin: 0 0 8px; }

.toast {
    position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
    transform: translate(-50%, 250%); transition: transform .25s, visibility .25s; visibility: hidden;
    background: var(--ok); color: #fff; padding: 16px 22px; border-radius: 14px;
    font-weight: 600; box-shadow: 0 10px 30px rgba(0, 0, 0, .25); max-width: 90vw; z-index: 20;
}
.toast.show { transform: translate(-50%, 0); visibility: visible; }
.toast.bad { background: #9a2a2a; }

/* ---------- Pages ---------- */
.page { max-width: 1100px; margin: 0 auto; padding: 22px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { font-family: "Brand", Georgia, serif; font-size: 30px; margin: 0; }
.page-head .sub { margin: 4px 0 0; color: var(--muted); }
.actions { display: flex; gap: 12px; }
.flash { background: #e9f1e6; border: 1px solid #c4dcbf; color: #23502d; padding: 14px 18px; border-radius: 12px; margin-bottom: 18px; font-weight: 600; }
.empty-state { text-align: center; padding: 60px 20px; background: var(--card); border: 2px dashed var(--line); border-radius: var(--radius); color: var(--muted); }
.empty-state p { font-size: 20px; margin: 0 0 18px; }

.sheet-block { margin-bottom: 26px; }
.sheet-block h2, .history h2 { font-size: 15px; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-2); margin: 0 0 10px; }
.sheet {
    display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr;
    aspect-ratio: 11 / 8.5; background: #fff; border-radius: 10px;
    box-shadow: 0 1px 0 var(--line), 0 10px 24px rgba(60, 40, 10, .1);
    overflow: hidden;
}
.quad { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 10px; border: 1px dashed #e8e0d0; min-width: 0; }
.quad.open { color: #c3b8a4; font-weight: 600; background: repeating-linear-gradient(45deg, #fff, #fff 10px, #faf6ee 10px, #faf6ee 20px); }
.quad-art { width: 78%; display: flex; }
.quad-art img { width: 100%; height: 100%; object-fit: contain; }
.quad-meta { width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 6px; margin-top: 6px; font-size: 14px; }
.quad-meta div { min-width: 0; display: flex; flex-direction: column; }
.quad-meta span { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 44px; height: 44px; flex: none; border-radius: 50%; border: 1px solid var(--line); background: var(--card); font-size: 24px; line-height: 1; color: #9a2a2a; }

.history { margin-top: 34px; }
.history ul { list-style: none; margin: 0; padding: 0; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); }
.history li { display: flex; align-items: center; gap: 16px; padding: 10px 18px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.history li:first-child { border-top: 0; }
.history li > :first-child { font-weight: 600; min-width: 110px; }
.history li a, .history li form { margin-left: auto; font-weight: 600; text-decoration: none; min-height: 44px; display: inline-flex; align-items: center; }

/* Graphics manager */
.upload-card { display: grid; grid-template-columns: 1.2fr 1fr auto; gap: 12px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 22px; }
@media (max-width: 760px) { .upload-card { grid-template-columns: 1fr; } }
.file-pick { position: relative; display: flex; align-items: center; justify-content: center; min-height: var(--tap); border: 2px dashed var(--gold); border-radius: 12px; color: var(--ink-2); font-weight: 600; padding: 0 12px; text-align: center; }
.file-pick input { position: absolute; inset: 0; opacity: 0; width: 100%; }
.g-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; }
.g-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.g-img { height: 130px; display: flex; align-items: center; justify-content: center; background: #fff; border-radius: 10px; }
.g-img img { max-width: 100%; max-height: 100%; }
.g-rename { display: flex; gap: 6px; }
.g-rename input[type=text] { flex: 1; min-width: 0; font-size: 17px; min-height: 44px; padding: 0 10px; }
.g-hide { text-align: right; }
