/* ===========================================================
   AI.CARD — Card Customizer
   =========================================================== */
:root {
    --bg: #0b1220;
    --bg-2: #111a2e;
    --panel: #ffffff;
    --ink: #0f172a;
    --muted: #64748b;
    --line: #e2e8f0;
    --brand: #0ea5e9;
    --brand-2: #0284c7;
    --brand-ink: #082f49;
    --danger: #ef4444;
    --ok: #10b981;
    --radius: 14px;
    --shadow: 0 10px 30px rgba(15, 23, 42, .10);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: #f1f5f9;
    color: var(--ink);
    font-family: 'Anuphan', 'Sarabun', 'Segoe UI', Tahoma, sans-serif;
    font-size: 15px;
    line-height: 1.55;
}

.wrap { width: min(1320px, 94vw); margin: 0 auto; }

/* ---------- header / footer ---------- */
.site-header {
    background: linear-gradient(120deg, var(--bg) 0%, var(--bg-2) 100%);
    color: #fff;
    position: sticky; top: 0; z-index: 50;
    box-shadow: 0 2px 18px rgba(0,0,0,.25);
}
.site-header .wrap { display: flex; align-items: center; justify-content: space-between; height: 62px; }
.brand { display: flex; align-items: center; text-decoration: none; }
.brand-logo { height: 42px; width: auto; display: block; transition: opacity .18s; }
.brand:hover .brand-logo { opacity: .85; }
@media (max-width: 520px) { .brand-logo { height: 34px; } }
.site-header nav { display: flex; gap: 6px; align-items: center; }
.site-header nav a {
    color: #cbd5e1; text-decoration: none; font-size: 14px;
    padding: 8px 14px; border-radius: 999px; transition: .18s;
}
.site-header nav a:hover { background: rgba(255,255,255,.09); color: #fff; }
.site-header nav a.active { background: var(--brand); color: #fff; }
.lang-switch { border: 1px solid rgba(255,255,255,.25); }

.site-footer {
    margin-top: 60px; padding: 26px 0;
    background: var(--bg); color: #94a3b8; font-size: 13px;
}
.site-footer .wrap { display: flex; flex-wrap: wrap; gap: 8px; justify-content: space-between; }

/* ---------- hero ---------- */
.hero {
    background: linear-gradient(120deg, var(--bg) 0%, #0d2a4a 55%, #075985 100%);
    color: #fff; padding: 34px 0 30px; text-align: center;
}
.hero h1 { margin: 0 0 6px; font-size: clamp(22px, 3vw, 32px); font-weight: 700; }
.hero p { margin: 0; color: #bae6fd; font-size: 15px; }

/* ---------- studio layout ---------- */
.studio {
    display: grid;
    grid-template-columns: 330px minmax(0, 1fr);
    gap: 22px;
    margin-top: 24px;
    align-items: start;
}
@media (max-width: 980px) { .studio { grid-template-columns: 1fr; } }

/* ---------- tool panel ---------- */
.panel {
    background: var(--panel); border-radius: var(--radius);
    box-shadow: var(--shadow); overflow: hidden;
    position: sticky; top: 78px;
    max-height: calc(100vh - 100px); overflow-y: auto;
}
@media (max-width: 980px) { .panel { position: static; max-height: none; } }

.tool-group { border-bottom: 1px solid var(--line); }
.tool-head {
    width: 100%; text-align: left; background: none; border: 0; cursor: pointer;
    padding: 14px 16px; font: inherit; font-weight: 600; color: var(--ink);
    display: flex; justify-content: space-between; align-items: center;
}
.tool-head::after { content: '▾'; color: var(--muted); transition: .2s; }
.tool-group.open .tool-head::after { transform: rotate(180deg); }
.tool-head:hover { background: #f8fafc; }
.tool-body { display: none; padding: 4px 16px 18px; }
.tool-group.open .tool-body { display: block; }

.lbl { display: block; font-size: 12.5px; color: var(--muted); margin: 12px 0 6px; font-weight: 500; }
.lbl .val { float: right; color: var(--ink); font-weight: 600; }
.hint { font-size: 12px; color: var(--muted); margin: 8px 0 0; }

.chk { display: flex; align-items: center; gap: 8px; font-size: 13.5px; margin-top: 12px; cursor: pointer; }
.chk input { width: 16px; height: 16px; accent-color: var(--brand); }

.row { display: flex; gap: 10px; }
.col { flex: 1; min-width: 0; }
.col.narrow { flex: 0 0 78px; }

input[type=color] {
    width: 100%; height: 38px; padding: 2px; border: 1px solid var(--line);
    border-radius: 9px; background: #fff; cursor: pointer;
}
input[type=text], input[type=tel], input[type=email], input[type=number], select, textarea {
    width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px;
    font: inherit; font-size: 14px; background: #fff; color: var(--ink);
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus {
    outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(14,165,233,.15);
}
input[type=range] { width: 100%; accent-color: var(--brand); }

/* segmented control */
.seg { display: flex; background: #f1f5f9; padding: 4px; border-radius: 10px; gap: 4px; }
.seg-btn {
    flex: 1; border: 0; background: none; padding: 7px 6px; border-radius: 7px;
    font: inherit; font-size: 13px; cursor: pointer; color: var(--muted); transition: .16s;
}
.seg-btn.active { background: #fff; color: var(--ink); font-weight: 600; box-shadow: 0 1px 4px rgba(0,0,0,.10); }

/* buttons */
.btn {
    border: 0; border-radius: 10px; padding: 11px 18px; font: inherit; font-weight: 600;
    cursor: pointer; transition: .16s; display: inline-flex; align-items: center;
    justify-content: center; gap: 6px; white-space: nowrap;
}
.btn.sm { padding: 8px 12px; font-size: 13px; }
.btn.lg { padding: 13px 26px; font-size: 15.5px; }
.btn.block { width: 100%; margin-top: 12px; }
.btn-primary { background: linear-gradient(120deg, var(--brand), var(--brand-2)); color: #fff; box-shadow: 0 4px 14px rgba(14,165,233,.35); }
.btn-primary:hover { filter: brightness(1.07); transform: translateY(-1px); }
.btn-ghost { background: #f1f5f9; color: var(--ink); }
.btn-ghost:hover { background: #e2e8f0; }
.btn.danger, .btn-ghost.danger { color: var(--danger); }
.btn:disabled { opacity: .55; cursor: not-allowed; transform: none; }
.btn-row { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; }

.icon-row { display: flex; gap: 6px; margin-top: 12px; align-items: center; }
.icon-row .spacer { flex: 1; }
.ibtn {
    width: 36px; height: 36px; border: 1px solid var(--line); background: #fff;
    border-radius: 9px; cursor: pointer; font: inherit; font-size: 15px; color: var(--ink);
    display: inline-flex; align-items: center; justify-content: center; transition: .15s;
}
.ibtn:hover { background: #f1f5f9; border-color: #cbd5e1; }
.ibtn.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.ibtn.danger { color: var(--danger); }
.ibtn.wide { width: 100%; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 6px; }

.presets { display: grid; grid-template-columns: repeat(6, 1fr); gap: 7px; margin-top: 4px; }
.preset {
    aspect-ratio: 1; border-radius: 8px; border: 2px solid transparent;
    cursor: pointer; box-shadow: 0 1px 4px rgba(0,0,0,.12);
}
.preset:hover { border-color: var(--brand); transform: scale(1.07); }

/* ---------- stage ---------- */
.stage-col { min-width: 0; }
.stage {
    background:
        linear-gradient(45deg, #e2e8f0 25%, transparent 25%) 0 0/22px 22px,
        linear-gradient(-45deg, #e2e8f0 25%, transparent 25%) 0 11px/22px 22px,
        linear-gradient(45deg, transparent 75%, #e2e8f0 75%) 11px -11px/22px 22px,
        linear-gradient(-45deg, transparent 75%, #e2e8f0 75%) -11px 0/22px 22px,
        #f8fafc;
    border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 28px; display: flex; align-items: center; justify-content: center;
    min-height: 420px; overflow: auto;
}
.canvas-container { box-shadow: 0 14px 40px rgba(15,23,42,.28); border-radius: 8px; }
.stage-actions { display: flex; gap: 12px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }

/* ---------- modal ---------- */
.modal {
    position: fixed; inset: 0; background: rgba(8, 14, 26, .72);
    backdrop-filter: blur(4px); z-index: 100;
    display: none; align-items: center; justify-content: center; padding: 20px;
}
.modal.show { display: flex; }
.modal-box {
    background: #fff; border-radius: 18px; padding: 28px;
    width: min(620px, 96vw); max-height: 92vh; overflow-y: auto; position: relative;
    box-shadow: 0 24px 70px rgba(0,0,0,.4);
    animation: pop .22s ease;
}
.modal-box.wide { width: min(880px, 96vw); }
.modal-box.narrow { width: min(440px, 96vw); }
.modal-box.center { text-align: center; }
@keyframes pop { from { opacity: 0; transform: translateY(14px) scale(.98); } }
.modal-box h2 { margin: 0 0 6px; font-size: 21px; }
.muted { color: var(--muted); font-size: 14px; margin: 0 0 18px; }
.modal-x {
    position: absolute; top: 14px; right: 16px; border: 0; background: none;
    font-size: 26px; line-height: 1; color: var(--muted); cursor: pointer;
}
.modal-x:hover { color: var(--ink); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

/* ---------- 3D preview ---------- */
.scene {
    perspective: 1600px; height: 420px; display: flex; align-items: center; justify-content: center;
    background: radial-gradient(ellipse at 50% 55%, #1e293b 0%, #0b1220 70%);
    border-radius: 14px; margin: 6px 0 18px; cursor: grab; user-select: none; overflow: hidden;
}
.scene:active { cursor: grabbing; }
/* NOTE: no `filter` on .card3d — a filter flattens the 3D rendering context in
   Chrome/Safari, which breaks backface-visibility and makes the front artwork
   show through the back face mirrored. Shadows live on the faces instead. */
.card3d {
    position: relative;
    transform-style: preserve-3d;
    -webkit-transform-style: preserve-3d;
    transition: transform .1s linear;
    will-change: transform;
}
.card3d .face {
    position: absolute; inset: 0;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    border-radius: 14px;
    overflow: hidden;
    background: #ffffff;
    box-shadow: 0 26px 34px rgba(0,0,0,.55);
}
.card3d .face.front { transform: rotateY(0deg) translateZ(1px); }
.card3d .face.back  { transform: rotateY(180deg) translateZ(1px); }
.card3d .face img {
    width: 100%; height: 100%; display: block; object-fit: cover;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}
.back-inner {
    width: 100%; height: 100%;
    background: #ffffff;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}
.preview-actions { display: flex; gap: 12px; align-items: center; justify-content: flex-end; flex-wrap: wrap; }
.preview-actions .chk { margin-top: 0; margin-right: auto; }

/* ---------- order form ---------- */
.order-grid { display: grid; grid-template-columns: 210px minmax(0,1fr); gap: 20px; }
@media (max-width: 620px) { .order-grid { grid-template-columns: 1fr; } }
.thumb-wrap {
    background: #f1f5f9; border-radius: 12px; padding: 12px;
    display: flex; align-items: flex-start; justify-content: center;
}
.thumb-wrap img { width: 100%; border-radius: 8px; box-shadow: 0 6px 18px rgba(15,23,42,.22); display: block; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; }
.err { display: block; color: var(--danger); font-size: 12.5px; margin-top: 4px; min-height: 1px; }
.field.invalid input, .field.invalid textarea { border-color: var(--danger); }

.qty { display: flex; align-items: center; gap: 0; max-width: 190px; }
.qty input { text-align: center; border-radius: 0; border-left: 0; border-right: 0; }
.qty-btn {
    width: 40px; height: 38px; border: 1px solid var(--line); background: #f8fafc;
    cursor: pointer; font-size: 17px; color: var(--ink);
}
.qty-btn:first-child { border-radius: 9px 0 0 9px; }
.qty-btn:last-child { border-radius: 0 9px 9px 0; }
.qty-presets { display: flex; gap: 6px; margin-top: 8px; }
.qty-presets button {
    border: 1px solid var(--line); background: #fff; border-radius: 7px;
    padding: 4px 12px; font: inherit; font-size: 13px; cursor: pointer;
}
.qty-presets button:hover { border-color: var(--brand); color: var(--brand-2); }

.form-msg { margin-top: 12px; font-size: 14px; color: var(--danger); min-height: 20px; }

.tick {
    width: 66px; height: 66px; border-radius: 50%; margin: 4px auto 14px;
    background: rgba(16,185,129,.12); color: var(--ok);
    display: flex; align-items: center; justify-content: center; font-size: 34px;
}
.order-no {
    background: #f1f5f9; border-radius: 10px; padding: 12px; margin: 16px 0 20px;
    display: flex; flex-direction: column; gap: 2px;
}
.order-no span { font-size: 12.5px; color: var(--muted); }
.order-no strong { font-size: 17px; letter-spacing: .5px; }

/* ---------- busy overlay ---------- */
.overlay {
    position: fixed; inset: 0; z-index: 200; background: rgba(8,14,26,.78);
    display: none; flex-direction: column; align-items: center; justify-content: center;
    gap: 16px; color: #e2e8f0; backdrop-filter: blur(3px);
}
.overlay.show { display: flex; }
.spinner {
    width: 44px; height: 44px; border-radius: 50%;
    border: 4px solid rgba(255,255,255,.2); border-top-color: var(--brand);
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
