:root {
  --bg: #fbf5ec;
  --surface: #ffffff;
  --text: #3b2614;
  --muted: #8c7461;
  --brown: #8b5a2b;
  --brown-dark: #6b4220;
  --accent: #e8a33d;
  --line: #eadccb;
  --green: #3f9c5a;
  --red: #c8483b;
  --busy: #fff1dc;
  --shadow: 0 2px 10px rgba(107, 66, 32, 0.08);
  --paper: #fcfcfa;
  --paper-dot: rgba(60, 60, 70, 0.07);
  --perf: rgba(60, 60, 70, 0.22);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1510;
    --surface: #2a1f17;
    --text: #f3e6d6;
    --muted: #b39a85;
    --brown: #c08552;
    --brown-dark: #e0a46c;
    --line: #3d2e22;
    --busy: #3a2a18;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    --paper: #eceae6;
    --paper-dot: rgba(60, 60, 70, 0.08);
    --perf: rgba(60, 60, 70, 0.25);
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}
.hidden { display: none !important; }

.top {
  background: linear-gradient(135deg, #ffd86b, #f4b63f);
  color: #3b2614;
  padding: 18px 16px 0;
}
.brand { display: flex; align-items: center; gap: 12px; max-width: 900px; margin: 0 auto; }
.logo { width: 52px; height: 52px; filter: drop-shadow(0 2px 3px rgba(59,38,20,.35)); animation: wobble 2.4s ease-in-out infinite; }
@keyframes wobble { 0%,100% { transform: rotate(-6deg); } 50% { transform: rotate(6deg); } }
h1 { margin: 0; font-size: 1.5rem; }
.tagline { margin: 0; opacity: 0.85; font-size: 0.9rem; }
.tabs { display: flex; gap: 4px; max-width: 900px; margin: 14px auto 0; overflow-x: auto; }
.tab {
  flex: 1 0 auto;
  background: transparent; color: #6b4220;
  border: 0; padding: 10px 14px; font-size: 0.95rem; cursor: pointer;
  border-radius: 10px 10px 0 0; font-weight: 600;
}
.tab:hover { background: rgba(255,255,255,.35); }
.tab.active { background: var(--bg); color: var(--text); }

main { max-width: 900px; margin: 0 auto; padding: 16px; }
.panel { display: none; }
.panel.active { display: block; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.card h2 { margin: 0; font-size: 1.1rem; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.card.occupied { border-color: var(--accent); }
.badge { background: var(--accent); color: #3b2614; border-radius: 999px; padding: 2px 10px; font-weight: 700; }
.hint { margin: 0 0 12px; color: var(--muted); font-size: 0.9rem; }
.empty { color: var(--muted); text-align: center; margin: 8px 0; }

input, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row input { flex: 1 1 180px; }

.btn {
  font: inherit; font-weight: 600; border: 0; border-radius: 10px; padding: 8px 16px; cursor: pointer;
}
.btn.primary { background: var(--brown); color: #fff; }
.btn.primary:hover { background: var(--brown-dark); }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.btn.danger { background: transparent; color: var(--red); border: 1px solid var(--red); padding: 4px 10px; font-size: 0.85rem; }

/* Active list */
.active-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.active-list li {
  display: flex; align-items: center; gap: 12px;
  background: var(--busy); border-radius: 12px; padding: 10px 12px;
}
.active-list .ava { font-size: 1.8rem; animation: wobble 1.2s ease-in-out infinite; }
.active-list .who { flex: 1; }
.active-list .who b { display: block; }
.active-list .who small { color: var(--muted); }
.timer { font-variant-numeric: tabular-nums; font-weight: 800; font-size: 1.4rem; color: var(--brown-dark); }
.timer.long { color: var(--red); }

/* Member grid */
.member-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.member {
  font: inherit; color: var(--text);
  background: var(--bg); border: 2px solid var(--line); border-radius: 14px;
  padding: 14px 8px; cursor: pointer; text-align: center;
  transition: transform 0.1s, border-color 0.15s;
}
.member:hover { border-color: var(--brown); }
.member:active { transform: scale(0.96); }
.member .ava { font-size: 2.2rem; display: block; }
.member .nm { font-weight: 600; display: block; margin-top: 4px; word-break: break-word; }
.member .st { display: block; font-size: 0.8rem; color: var(--muted); margin-top: 2px; }
.member.busy { background: var(--busy); border-color: var(--accent); }
.member.busy .st { color: var(--red); font-weight: 700; }
.member:disabled { opacity: 0.6; cursor: wait; }

/* Rank */
.seg { display: flex; gap: 6px; margin-bottom: 16px; flex-wrap: wrap; }
.seg button {
  font: inherit; flex: 1; padding: 8px 10px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
}
.seg button.on { background: var(--brown); color: #fff; border-color: var(--brown); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px; text-align: center; }
.stat .v { font-size: 1.3rem; font-weight: 800; color: var(--brown-dark); font-variant-numeric: tabular-nums; }
.stat .l { font-size: 0.8rem; color: var(--muted); }
.record { display: flex; align-items: center; gap: 12px; }
.record .big { font-size: 2.4rem; }
.record b { color: var(--brown-dark); }

.rank-list { list-style: none; margin: 0; padding: 0; }
.rank-list li {
  display: grid; grid-template-columns: 36px 1fr auto; align-items: center; gap: 10px;
  padding: 10px 4px; border-bottom: 1px solid var(--line);
}
.rank-list li:last-child { border-bottom: 0; }
.rank-list .pos { font-size: 1.3rem; text-align: center; font-weight: 800; color: var(--muted); }
.rank-list .name b { display: block; }
.rank-list .name small { color: var(--muted); }
.rank-list .val { font-weight: 800; font-variant-numeric: tabular-nums; color: var(--brown-dark); }
.bar { height: 6px; background: var(--line); border-radius: 3px; margin-top: 4px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--brown); border-radius: 3px; }

/* History */
.table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.table th, .table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
.table th { color: var(--muted); font-weight: 600; font-size: 0.85rem; }
.table td.dur { font-weight: 700; font-variant-numeric: tabular-nums; }
body:not(.is-admin) .admin-only { display: none; }

/* Admin */
.admin-members { list-style: none; margin: 10px 0 0; padding: 0; }
.admin-members li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.admin-members li span.n { flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(120px);
  background: #3b2614; color: #fff; padding: 10px 18px; border-radius: 999px;
  transition: transform 0.25s; max-width: calc(100% - 32px); text-align: center; z-index: 10;
}
.toast.show { transform: translateX(-50%) translateY(0); }

@media (max-width: 520px) {
  .stats { grid-template-columns: 1fr 1fr 1fr; }
  .stat .v { font-size: 1.05rem; }
  .tab { padding: 10px 10px; font-size: 0.85rem; }
}

/* ===== Cuộn giấy vệ sinh (thật hơn) ===== */
:root { --noise: url("data:image/svg+xml,%3Csvg%20xmlns%3D%27http%3A//www.w3.org/2000/svg%27%20width%3D%27180%27%20height%3D%27180%27%3E%3Cfilter%20id%3D%27n%27%3E%3CfeTurbulence%20type%3D%27fractalNoise%27%20baseFrequency%3D%270.85%27%20numOctaves%3D%273%27%20stitchTiles%3D%27stitch%27/%3E%3CfeColorMatrix%20values%3D%270%200%200%200%200.35%200%200%200%200%200.35%200%200%200%200%200.38%200%200%200%200.09%200%27/%3E%3C/filter%3E%3Crect%20width%3D%27100%25%27%20height%3D%27100%25%27%20filter%3D%27url%28%23n%29%27/%3E%3C/svg%3E"); --torn: url("data:image/svg+xml,%3Csvg%20xmlns%3D%27http%3A//www.w3.org/2000/svg%27%20viewBox%3D%270%200%20120%2014%27%20preserveAspectRatio%3D%27none%27%3E%3Cpath%20d%3D%27M0%200%20H120%20V6%20L117.5%204.5%20L114.1%203.7%20L111.0%206.7%20L109.3%208.1%20L107.7%207.3%20L106.0%203.9%20L103.2%2011.3%20L101.3%205.2%20L97.9%2012.5%20L94.7%207.0%20L90.3%203.5%20L86.2%205.9%20L84.3%204.2%20L81.9%2011.2%20L79.8%208.8%20L76.4%206.7%20L73.2%203.6%20L71.6%205.1%20L68.0%207.3%20L65.6%208.9%20L62.7%206.0%20L58.8%2010.0%20L56.6%208.7%20L53.5%2011.8%20L49.8%205.9%20L45.4%204.2%20L42.7%2010.6%20L40.7%207.9%20L39.1%209.7%20L35.3%208.7%20L31.2%206.1%20L27.6%208.9%20L24.3%207.6%20L20.3%2012.4%20L17.4%209.6%20L15.7%2010.0%20L12.3%2012.9%20L8.3%205.8%20L5.6%209.7%20L4.1%207.6%20L2.1%204.2%20L0.4%2010.7%20L0.0%205.5%20Z%27%20fill%3D%27black%27/%3E%3C/svg%3E"); }
.roll {
  max-width: 930px; margin: 12px auto 0; padding: 0 6px;
  position: relative; z-index: 3; display: flex; align-items: center; height: 48px;
  filter: drop-shadow(0 6px 6px rgba(30, 30, 40, 0.25));
}
.roll-body {
  flex: 1; height: 48px; margin-left: -17px; border-radius: 0 24px 24px 0 / 0 24px 24px 0;
  background:
    var(--noise),
    radial-gradient(circle, rgba(70, 70, 80, 0.08) 0 1.2px, transparent 1.8px) 0 0 / 14px 14px,
    linear-gradient(180deg, #d9d9d4 0%, #f4f4f1 10%, #ffffff 24%, #fbfbf9 42%, #ececE8 62%, #d6d6d1 82%, #b9b9b3 100%);
}
.roll-end {                         /* đầu cuộn: lớp giấy + lõi carton */
  position: relative; z-index: 1; flex: 0 0 34px; height: 48px; border-radius: 50%;
  background:
    radial-gradient(ellipse closest-side, #241810 0 22%, #6b4a2c 26%, #a87e52 31%, #c9a273 38%, transparent 40%),
    repeating-radial-gradient(ellipse closest-side, #fbfbf8 0 1.6px, #e3e3de 1.6px 2.4px),
    #f2f2ee;
  box-shadow: inset -4px 0 6px rgba(0, 0, 0, 0.12), inset 2px 0 2px rgba(255, 255, 255, 0.9), 0 0 0 1px rgba(0, 0, 0, 0.06);
}

/* ===== Tờ giấy ===== */
main.paper {
  --pad: 20px;
  position: relative;
  width: calc(100% - 32px);
  max-width: 880px;
  margin: -24px auto 56px;          /* giấy chui ra từ dưới cuộn */
  padding: 40px var(--pad) 30px;
  background-color: var(--paper);
  background-image:
    var(--noise),
    radial-gradient(circle, rgba(70, 70, 80, 0.11) 0 1.2px, transparent 2px),
    radial-gradient(circle, rgba(70, 70, 80, 0.11) 0 1.2px, transparent 2px),
    radial-gradient(ellipse at 40% 35%, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0) 65%);
  background-size: 180px 180px, 18px 18px, 18px 18px, 9px 9px;
  background-position: 0 0, 0 0, 9px 9px, 0 0;
  box-shadow:
    inset 0 18px 14px -12px rgba(30, 30, 40, 0.28),   /* bóng của cuộn giấy */
    inset 1px 0 0 rgba(0, 0, 0, 0.04), inset -1px 0 0 rgba(0, 0, 0, 0.04),
    0 2px 2px rgba(30, 30, 40, 0.06),
    0 14px 30px rgba(30, 30, 40, 0.22);
  /* giấy luôn sáng: ép màu chữ/ô bên trong theo tông sáng */
  --text: #3b2614; --muted: #7a6d63; --bg: #f6f5f2; --line: #e4e1dc;
  --busy: #fff3df; --brown: #8b5a2b; --brown-dark: #6b4220;
  color: var(--text);
  transform-origin: top center;
  animation: unroll 0.9s cubic-bezier(.2, .8, .3, 1) both;
}
main.paper::after {                 /* mép giấy xé không đều */
  content: ""; position: absolute; left: 0; right: 0; bottom: -13px; height: 14px;
  background: var(--paper);
  -webkit-mask: var(--torn) 0 0 / 120px 14px repeat-x;
  mask: var(--torn) 0 0 / 120px 14px repeat-x;
}
main.paper::before {                /* bóng của mép xé */
  content: ""; position: absolute; left: 4px; right: 4px; bottom: -14px; height: 14px;
  background: rgba(30, 30, 40, 0.12); filter: blur(3px);
  -webkit-mask: var(--torn) 0 2px / 120px 14px repeat-x;
  mask: var(--torn) 0 2px / 120px 14px repeat-x;
}
@keyframes unroll { from { transform: scaleY(0.05); opacity: 0; } to { transform: scaleY(1); opacity: 1; } }

/* Mỗi ô là một tờ, ngăn bằng đường đục lỗ */
main.paper .card {
  position: relative;
  background: transparent; border: 0; border-radius: 0; box-shadow: none;
  margin: 0; padding: 20px 2px;
}
main.paper .panel > .card:not(:last-child)::after {
  content: ""; position: absolute; left: calc(-1 * var(--pad)); right: calc(-1 * var(--pad)); bottom: -2px; height: 4px;
  background:
    repeating-linear-gradient(90deg, rgba(60, 60, 70, 0.32) 0 2.5px, transparent 2.5px 6px) 0 1px / 100% 2px no-repeat,
    linear-gradient(180deg, rgba(0, 0, 0, 0.05), rgba(255, 255, 255, 0.9));
}
main.paper .card.occupied .card-head h2 { color: var(--brown-dark); }
main.paper .stat { background: rgba(255, 255, 255, 0.5); border: 1px solid rgba(60, 60, 70, 0.12); }
main.paper .seg button:not(.on) { background: rgba(255, 255, 255, 0.6); }

@media (max-width: 520px) {
  main.paper { --pad: 12px; width: calc(100% - 24px); padding: 36px var(--pad) 26px; }
}
@media (prefers-reduced-motion: reduce) { main.paper { animation: none; } }

/* ===== Bồn cầu nhìn từ trên xuống (thật hơn) ===== */
:root { --tile: #efe7dc; --grout: rgba(107, 66, 32, 0.1); }
@media (prefers-color-scheme: dark) { :root { --tile: #241b15; --grout: rgba(255, 230, 200, 0.05); } }
body {
  background-color: var(--tile);
  background-image:
    linear-gradient(var(--grout) 2px, transparent 2px),
    linear-gradient(90deg, var(--grout) 2px, transparent 2px);
  background-size: 72px 72px;
}
.stage { position: relative; padding-top: 1px; overflow-x: clip; }
.stage > .roll, .stage > main.paper { position: relative; }
.stage > main.paper { z-index: 2; }

.bowl {                             /* bệ ngồi (men sứ) */
  --rim: 54px;
  position: absolute; z-index: 0;
  top: 22px; left: 50%; transform: translateX(-50%);
  width: min(1500px, calc(100% - 40px));
  aspect-ratio: 1.2 / 1;
  border-radius: 50%;
  background:
    radial-gradient(ellipse at 30% 18%, rgba(255, 255, 255, 1), rgba(255, 255, 255, 0) 45%),
    linear-gradient(165deg, #ffffff 0%, #f3f5f5 40%, #dfe3e4 75%, #c8cdcf 100%);
  box-shadow:
    inset 0 6px 8px rgba(255, 255, 255, 0.9),
    0 0 0 1px rgba(150, 160, 165, 0.35);
}
.bowl::before {                     /* vệt sáng chạy dọc mặt bệ */
  content: ""; position: absolute; inset: calc(var(--rim) * 0.22); border-radius: 50%;
  border: calc(var(--rim) * 0.18) solid transparent;
  border-top-color: rgba(255, 255, 255, 0.95);
  border-left-color: rgba(255, 255, 255, 0.6);
  filter: blur(5px); pointer-events: none;
}
.basin {                            /* lòng bồn */
  position: absolute; inset: var(--rim); border-radius: 50%; overflow: hidden;
  background: radial-gradient(ellipse 62% 66% at 50% 58%, #c4cfd4 0%, #e9eef0 30%, #f6f8f8 52%, #e2e8ea 74%, #b7c2c7 92%, #9eabb1 100%);
  box-shadow:
    0 0 0 2px #d3d9db,                               /* gờ trong của bệ */
    0 0 0 4px rgba(255, 255, 255, 0.8);
}
.glare {                            /* phản chiếu trên men sứ */
  position: absolute; left: 7%; top: 16%; width: 22%; height: 56%; border-radius: 50%;
  border-left: 16px solid rgba(255, 255, 255, 0.8);
  filter: blur(7px); transform: rotate(12deg);
}
.glare::after {
  content: ""; position: absolute; right: -330%; top: 10%; width: 60%; height: 80%; border-radius: 50%;
  border-right: 8px solid rgba(255, 255, 255, 0.5);
}
.water {                            /* nước: sâu dần vào lỗ thoát */
  position: absolute; left: 50%; top: 60%; transform: translate(-50%, -50%);
  width: 50%; height: 50%; border-radius: 50%; overflow: hidden;
  background:
    radial-gradient(ellipse 34% 30% at 50% 74%, rgba(40, 95, 110, 0.55) 0%, rgba(90, 150, 165, 0.25) 55%, transparent 100%),
    radial-gradient(ellipse at 50% 60%, #b9dbe4 0%, #cfe7ee 45%, #dcedf2 75%, #e6f2f5 100%);
  box-shadow:
    inset 0 0 30px rgba(60, 120, 140, 0.25),
    inset 0 14px 22px rgba(255, 255, 255, 0.35),
    0 0 0 2px rgba(255, 255, 255, 0.55),            /* viền mặt nước */
    0 0 10px 3px rgba(120, 170, 190, 0.25);
}
.water::before {                    /* ánh sáng phản chiếu mặt nước */
  content: ""; position: absolute; left: 16%; top: 10%; width: 46%; height: 20%; border-radius: 50%;
  background: radial-gradient(ellipse, rgba(255, 255, 255, 0.75), rgba(255, 255, 255, 0) 70%);
  transform: rotate(-12deg);
}
.water::after {                     /* gợn sóng */
  content: ""; position: absolute; left: 22%; top: 30%; width: 26%; height: 22%; border-radius: 50%;
  border: 1.5px solid rgba(255, 255, 255, 0.5);
  animation: ripple 4s ease-out infinite;
}
.drain {                            /* lỗ thoát nước */
  position: absolute; left: 50%; top: 76%; width: 15%; height: 12%; transform: translate(-50%, -50%);
  border-radius: 50%; background: radial-gradient(ellipse, rgba(10, 35, 42, 0.85) 0 35%, rgba(30, 75, 88, 0.45) 65%, transparent 100%);
  filter: blur(3px);
}
@keyframes ripple { from { transform: scale(0.4); opacity: 1; } to { transform: scale(1.6); opacity: 0; } }

@media (max-width: 520px) {
  .bowl { --rim: 16px; top: 26px; width: calc(100% - 20px); aspect-ratio: 0.55 / 1; }
  .glare { border-left-width: 8px; }
}
@media (prefers-reduced-motion: reduce) { .water::after { animation: none; } }

.top { position: relative; z-index: 5; box-shadow: 0 4px 12px rgba(59, 38, 20, 0.18); }

/* ===== Tab dạng nút, tương phản rõ trên nền vàng ===== */
.top { padding-bottom: 14px; }
.tabs { gap: 8px; padding: 2px; }
.tab {
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.55);
  color: #4a2e14;
  border: 2px solid rgba(107, 66, 32, 0.18);
  font-weight: 700;
}
.tab:hover { background: rgba(255, 255, 255, 0.85); }
.tab.active {
  background: #6b4220; color: #fff; border-color: #6b4220;
  box-shadow: 0 3px 8px rgba(59, 38, 20, 0.3);
}

/* ===== Ảnh avatar ===== */
.ava-img {
  width: 1.3em; height: 1.3em; border-radius: 50%; object-fit: cover;
  vertical-align: middle; display: inline-block;
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(59, 38, 20, 0.25);
}
.member .ava .ava-img { width: 64px; height: 64px; }
.active-list .ava .ava-img { width: 46px; height: 46px; }
.rank-list .ava-img, .table .ava-img, .record .ava-img { width: 26px; height: 26px; border-width: 1px; }
.admin-members .ava { font-size: 1.6rem; width: 44px; text-align: center; }
.admin-members .ava-img { width: 40px; height: 40px; }
.btn.small { padding: 4px 10px; font-size: 0.85rem; }
.admin-members label.btn { cursor: pointer; }
.file-btn { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.preview { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; }
@media (max-width: 520px) { .admin-members li { flex-wrap: wrap; } }
.tabs { display: grid; grid-template-columns: repeat(4, 1fr); overflow: visible; }
.tab { white-space: nowrap; min-width: 0; }
@media (max-width: 520px) {
  .tabs { gap: 5px; }
  .tab { padding: 8px 2px; font-size: 0.78rem; border-width: 1.5px; }
}

/* ===== Cục cức nổi trong bồn ===== */
.turd {
  position: absolute; left: 50%; top: 48%; width: 22%; aspect-ratio: 1;
  transform: translate(-50%, -50%);
  animation: bob 4.5s ease-in-out infinite;
}
.turd img {
  position: relative; z-index: 1; width: 100%; height: 100%;
  filter: saturate(0.95);
}
.turd-shadow { display: none; }
.turd-ring {                        /* mặt nước ôm quanh chân cục cức */
  position: absolute; z-index: 2; left: -4%; right: -4%; bottom: 2%; height: 22%; border-radius: 50%;
  background: linear-gradient(180deg, rgba(200, 230, 238, 0) 0%, rgba(190, 222, 232, 0.55) 70%);
  box-shadow: 0 -1px 0 rgba(255, 255, 255, 0.6);
  animation: ring 4.5s ease-in-out infinite;
}
@keyframes bob {
  0%, 100% { transform: translate(-50%, -50%) rotate(-4deg); }
  50% { transform: translate(-46%, -53%) rotate(5deg); }
}
@keyframes ring { 0%, 100% { opacity: 0.8; } 50% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .turd, .turd-ring { animation: none; } }
@media (max-width: 520px) { .turd { width: 34%; top: 60%; } }
