:root {
  --bg: #f6f1e7;
  --card: #fffdf8;
  --ink: #2e2620;
  --muted: #7d7163;
  --accent: #c96f2e;
  --accent-hover: #b35e22;
  --accent-ink: #fff8f0;
  --teal: #4a8f8c;
  --border: #e5dccb;
  --danger: #b3402e;
  --shadow: 0 2px 10px rgba(60, 45, 25, 0.08);
  --checker: #eee8da;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #211d18;
    --card: #2b2620;
    --ink: #f0e9dd;
    --muted: #a89a89;
    --accent: #d98a44;
    --accent-hover: #e39a58;
    --accent-ink: #241a10;
    --teal: #63aca8;
    --border: #3a332b;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
    --checker: #332d26;
  }
}

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

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.55;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--teal); }

/* ---------- header ---------- */

header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0.9rem 1.2rem;
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.logo {
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--ink);
  text-decoration: none;
  letter-spacing: -0.02em;
}

.logo span { color: var(--accent); }

nav { margin-left: auto; display: flex; gap: 1.2rem; align-items: center; }

nav a { color: var(--ink); text-decoration: none; font-weight: 600; padding: 0.25rem 0.15rem; border-bottom: 2.5px solid transparent; }
nav a:hover { color: var(--accent); }
nav a.active { color: var(--accent); border-bottom-color: var(--accent); }

.cart-badge {
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.05rem 0.5rem;
  margin-left: 0.25rem;
  display: none;
}
.cart-badge.visible { display: inline-block; }

/* ---------- layout ---------- */

main {
  max-width: 1100px;
  width: 100%;
  margin: 0 auto;
  padding: 2rem 1.2rem 4rem;
  flex: 1;
}

footer {
  border-top: 1px solid var(--border);
  color: var(--muted);
  text-align: center;
  padding: 1.2rem;
  font-size: 0.9rem;
}
footer a { color: var(--muted); }

.hero { text-align: center; padding: 1.5rem 0 2.5rem; }
.hero h1 { font-size: 2.4rem; letter-spacing: -0.03em; }
.hero p { color: var(--muted); font-size: 1.1rem; margin-top: 0.4rem; }

/* ---------- category tabs ---------- */

.tabs { display: flex; gap: 0.6rem; justify-content: center; flex-wrap: wrap; margin-bottom: 1.8rem; }

.tab {
  font: inherit;
  font-weight: 700;
  padding: 0.45rem 1.1rem;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
  transition: all 0.15s ease;
}
.tab:hover { border-color: var(--accent); color: var(--accent); }
.tab.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.badge.cat { background: color-mix(in srgb, var(--teal) 18%, transparent); color: var(--teal); margin-right: 0.4rem; }

/* ---------- product grid ---------- */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 1.4rem;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.card:hover { transform: translateY(-3px); box-shadow: 0 6px 18px rgba(60, 45, 25, 0.14); }

.card a.img-link { display: block; }

.product-img {
  aspect-ratio: 1;
  width: 100%;
  object-fit: contain;
  padding: 1rem;
  background:
    repeating-conic-gradient(var(--checker) 0% 25%, transparent 0% 50%)
    50% / 24px 24px;
}

.card-body { padding: 1rem 1.1rem 1.2rem; display: flex; flex-direction: column; gap: 0.5rem; flex: 1; }
.card-body h3 { font-size: 1.05rem; }
.card-body h3 a { color: var(--ink); text-decoration: none; }
.card-body h3 a:hover { color: var(--accent); }

.price { font-weight: 800; color: var(--accent); font-size: 1.1rem; }
.stock-note { color: var(--muted); font-size: 0.85rem; }
.card-body .row { margin-top: auto; display: flex; align-items: center; justify-content: space-between; gap: 0.6rem; }

/* ---------- buttons & forms ---------- */

.btn {
  font: inherit;
  font-weight: 700;
  border: none;
  border-radius: 10px;
  padding: 0.55rem 1.1rem;
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-ink);
  transition: background 0.15s ease;
}
.btn:hover { background: var(--accent-hover); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary { background: transparent; color: var(--ink); border: 1.5px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #96311f; }
.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.85rem; border-radius: 8px; }

label { font-weight: 600; font-size: 0.9rem; display: block; margin-bottom: 0.25rem; }

input, textarea, select {
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 0.55rem 0.75rem;
  width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }

.field { margin-bottom: 1rem; }

/* ---------- product page ---------- */

.product-detail {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2.5rem;
  align-items: start;
}
@media (max-width: 720px) { .product-detail { grid-template-columns: 1fr; } }

.product-detail .product-img { border: 1px solid var(--border); border-radius: 16px; background-color: var(--card); }
.product-detail h1 { font-size: 2rem; letter-spacing: -0.02em; }
.product-detail .price { font-size: 1.6rem; margin: 0.6rem 0; display: block; }
.product-detail p.desc { color: var(--muted); margin-bottom: 1.5rem; }

.qty-row { display: flex; gap: 0.8rem; align-items: center; margin-bottom: 1rem; }
.qty-row input { width: 5rem; text-align: center; }

/* ---------- 3D viewer ---------- */

.stl-viewer {
  position: relative;
  aspect-ratio: 1;
  border: 1px solid var(--border);
  border-radius: 16px;
  background:
    repeating-conic-gradient(var(--checker) 0% 25%, transparent 0% 50%)
    50% / 24px 24px var(--card);
  overflow: hidden;
  touch-action: none;
}
.stl-viewer canvas { display: block; width: 100% !important; height: 100% !important; }
.stl-hint {
  position: absolute;
  bottom: 0.7rem;
  left: 0;
  right: 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  pointer-events: none;
}

/* ---------- card maker ---------- */

.maker-layout {
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
  gap: 2rem;
  align-items: start;
}
@media (max-width: 820px) { .maker-layout { grid-template-columns: 1fr; } }

.maker-preview { position: sticky; top: 5rem; display: flex; justify-content: center; }
.maker-preview canvas {
  width: min(460px, 100%);
  height: auto;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(20, 12, 4, 0.35);
  touch-action: none;
  cursor: grab;
}
.maker-preview canvas:active { cursor: grabbing; }

input[type="range"] { padding: 0; accent-color: var(--accent); }

.cart-item canvas.design-thumb {
  width: 58px;
  height: auto;
  border-radius: 6px;
  flex-shrink: 0;
}

/* ---------- cart ---------- */

.cart-list { display: flex; flex-direction: column; gap: 1rem; margin-bottom: 2rem; }

.cart-item {
  display: flex;
  gap: 1rem;
  align-items: center;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.8rem 1rem;
}
.cart-item img { width: 72px; height: 72px; object-fit: contain; border-radius: 10px; background: repeating-conic-gradient(var(--checker) 0% 25%, transparent 0% 50%) 50% / 16px 16px; }
.cart-item .info { flex: 1; }
.cart-item .info a { color: var(--ink); text-decoration: none; font-weight: 700; }
.cart-item input { width: 4.2rem; text-align: center; }

.cart-total { text-align: right; font-size: 1.3rem; margin-bottom: 2rem; }

.checkout-form { max-width: 480px; }

.panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.empty { text-align: center; color: var(--muted); padding: 3rem 0; font-size: 1.1rem; }

/* ---------- admin ---------- */

.admin-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.5rem; flex-wrap: wrap; gap: 0.8rem; }
.admin-header h1 { font-size: 1.7rem; }

.login-box { max-width: 380px; margin: 3rem auto; }
.login-box h1 { margin-bottom: 1.2rem; text-align: center; }

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 14px; overflow: hidden; }
th, td { text-align: left; padding: 0.7rem 0.9rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
tr:last-child td { border-bottom: none; }

.thumb { width: 52px; height: 52px; object-fit: contain; border-radius: 8px; background: repeating-conic-gradient(var(--checker) 0% 25%, transparent 0% 50%) 50% / 12px 12px; }

.badge { font-size: 0.75rem; font-weight: 700; padding: 0.15rem 0.6rem; border-radius: 999px; }
.badge.on { background: color-mix(in srgb, var(--teal) 20%, transparent); color: var(--teal); }
.badge.off { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }

section.admin-section { margin-bottom: 2.8rem; }
section.admin-section h2 { margin-bottom: 1rem; }

.order-card { margin-bottom: 1rem; }
.order-card .order-top { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 0.6rem; margin-bottom: 0.5rem; }
.order-card ul { margin: 0.5rem 0 0 1.2rem; color: var(--muted); }
.order-card select { width: auto; }

dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--card);
  color: var(--ink);
  padding: 1.8rem;
  width: min(480px, 92vw);
  margin: auto;
}
dialog::backdrop { background: rgba(20, 15, 8, 0.5); }
dialog h2 { margin-bottom: 1.2rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.7rem; margin-top: 1.4rem; }

/* ---------- toast ---------- */

#toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: var(--bg);
  padding: 0.7rem 1.4rem;
  border-radius: 12px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 100;
  max-width: 90vw;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.error { background: var(--danger); color: #fff; }
