:root {
  --bg: #15141a;
  --surface: #1e1c24;
  --surface-2: #26232d;
  --border: #37333f;
  --ink: #ece7e2;
  --ink-dim: #a89fb0;
  --ember: #e08a4f;
  --teal: #5fb0a3;
  --good: #7cc276;
  --warn: #e0c14f;
  --bad: #e2796a;
  --radius: 10px;
  --sans: -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Georgia, serif;
}

* {
  box-sizing: border-box; -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none; /* iOS: без этого долгий тап открывает системное меню выделения/копирования */
}

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-variant-numeric: tabular-nums;
  user-select: none;
  -webkit-user-select: none;
}

h1, h2 { font-family: var(--serif); font-weight: 600; margin: 0; }

button { font-family: var(--sans); cursor: pointer; }

/* --- login (тот же паттерн, что web/dashboard) --- */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
#login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem 1.8rem;
}
#login-card h1 { font-size: 1.3rem; margin-bottom: .3rem; }
#login-card p { color: var(--ink-dim); font-size: .88rem; margin: 0 0 1.4rem; }
#login-card label { display: block; font-size: .8rem; color: var(--ink-dim); margin: .9rem 0 .3rem; }
#login-card input {
  width: 100%; padding: .7rem .7rem; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 6px; color: var(--ink); font-size: 1rem; font-family: var(--sans);
}
#login-card button {
  width: 100%; margin-top: 1.4rem; padding: .75rem; border: none; border-radius: 6px;
  background: var(--ember); color: #1a1208; font-weight: 600; font-size: 1rem;
}
#login-error { color: var(--bad); font-size: .85rem; margin-top: .8rem; min-height: 1.2em; }

/* --- app shell --- */
#app { display: none; height: 100vh; flex-direction: column; }
#app.ready { display: flex; }

#topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: .8rem 1rem; border-bottom: 1px solid var(--border); background: var(--surface);
  flex: 0 0 auto;
}
.hall-tabs { display: flex; gap: .4rem; }
.hall-tabs button {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--ink-dim);
  padding: .5rem 1rem; border-radius: 8px; font-size: .95rem;
}
.hall-tabs button.active { background: var(--ember); color: #1a1208; border-color: var(--ember); font-weight: 600; }
.topbar-actions { display: flex; gap: .5rem; }
.ghost-btn {
  background: none; border: 1px solid var(--border); color: var(--ink-dim);
  padding: .5rem 1rem; border-radius: 8px; font-size: .9rem;
}
.help-btn {
  width: 2.2rem; padding: .5rem 0; font-weight: 700; color: var(--ember); border-color: var(--ember);
}
.hidden { display: none !important; }

.stats-bar {
  display: flex; gap: .5rem; padding: .5rem .8rem; background: var(--surface);
  border-bottom: 1px solid var(--border); flex: 0 0 auto; overflow-x: auto;
}
.stat-chip {
  flex: 1; min-width: 84px; text-align: center; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px; padding: .35rem .3rem;
}
.stat-chip .stat-value { display: block; font-size: 1.15rem; font-weight: 700; }
.stat-chip .stat-label { display: block; font-size: .65rem; color: var(--ink-dim); margin-top: .1rem; }

#map-screen { flex: 1 1 auto; position: relative; overflow: hidden; }
.map { position: absolute; inset: .5rem; }

.table-tile {
  position: absolute; overflow: hidden; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: .05rem;
  background: var(--surface-2); border: 2px solid var(--border); color: var(--ink);
  font-size: .85rem; font-weight: 600; text-align: center; padding: .2rem; line-height: 1.15;
  transition: background-color .3s, border-color .3s;
}
.table-tile.shape-rect { border-radius: 10px; }
.table-tile.shape-circle { border-radius: 50%; }
.table-tile .tile-title.occupied { color: var(--ember); }
.table-tile .tile-sub { font-size: .68rem; font-weight: 500; color: var(--ink-dim); }
.table-tile .tile-timer { font-size: 1.05rem; font-weight: 700; white-space: nowrap; }
.table-tile .tile-badge { font-size: .62rem; font-weight: 500; color: var(--ink-dim); margin-left: .2rem; }

/* Кнопка "Уголь" - обычный элемент в потоке (не absolute), чтобы не перекрывать таймер/счётчики
   при плотном контенте на маленьких тайлах (круглые "Бар N"). Стек тайла нарочно ограничен тремя
   строками (название / таймер+значки / кнопка) - на маленьких кругах больше не помещается. */
.tile-coal-btn {
  background: var(--warn); color: #2a2205; border: none; border-radius: 999px;
  font-size: .6rem; font-weight: 700; padding: .12rem .5rem; line-height: 1.2;
  box-shadow: 0 1px 4px rgba(0,0,0,.35);
}
.tile-coal-btn:active { filter: brightness(.92); }

/* Вспышка на срабатывание двойного тапа (см. DOUBLE_TAP_MS) - подтверждение, что кальян подан. */
.table-tile.tile-flash { box-shadow: 0 0 0 4px var(--ember) inset; }

.table-tile.state-green { background: color-mix(in srgb, var(--good) 30%, var(--surface-2)); border-color: var(--good); }
.table-tile.state-yellow { background: color-mix(in srgb, var(--warn) 30%, var(--surface-2)); border-color: var(--warn); }
.table-tile.state-red { background: color-mix(in srgb, var(--bad) 35%, var(--surface-2)); border-color: var(--bad); animation: pulse-red 1.4s infinite; }
@keyframes pulse-red {
  0%, 100% { box-shadow: 0 0 0 0 rgba(226, 121, 106, .55); }
  50% { box-shadow: 0 0 0 8px rgba(226, 121, 106, 0); }
}
.table-tile.layout-editing { cursor: grab; outline: 2px dashed var(--teal); outline-offset: 2px; }
.table-tile.dragging { cursor: grabbing; opacity: .85; z-index: 10; }

.layout-edit-hint {
  position: absolute; bottom: .8rem; left: 50%; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: .5rem 1rem; font-size: .85rem; color: var(--ink-dim);
}

/* --- экран стола (bottom sheet) --- */
.sheet-overlay, .menu-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 100;
  align-items: flex-end; justify-content: center;
}
.sheet-overlay.open, .menu-overlay.open { display: flex; }
.sheet {
  width: 100%; max-width: 720px; max-height: 88vh; overflow-y: auto;
  background: var(--surface); border-radius: 18px 18px 0 0; padding: 1.4rem 1.2rem 2rem;
  position: relative;
}
.sheet-close {
  position: absolute; top: .8rem; right: 1rem; background: none; border: none; color: var(--ink-dim);
  font-size: 1.6rem; line-height: 1;
}
.sheet h2 { font-size: 1.3rem; margin-bottom: .8rem; padding-right: 2rem; }

.table-stats {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem; margin-bottom: 1rem;
}
.table-stats .stat {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: .6rem .5rem;
  text-align: center;
}
.table-stats .stat .label { font-size: .72rem; color: var(--ink-dim); }
.table-stats .stat .value { font-size: 1.05rem; font-weight: 600; margin-top: .15rem; }

.hookah-list { display: flex; flex-direction: column; gap: .6rem; margin-bottom: 1.2rem; }
.hookah-card {
  position: relative; overflow: hidden; border-radius: 10px; border: 2px solid var(--border);
  background: var(--surface-2);
}
.hookah-card-inner {
  position: relative; z-index: 1; background: var(--surface-2);
  display: flex; align-items: center; justify-content: space-between; padding: .8rem 1rem;
  transition: transform .15s ease-out; touch-action: pan-y;
}
/* Подложка под карточкой кальяна: левая половина видна при свайпе вправо (зелёная, "Скурено"),
   правая - при свайпе влево (красная, "Отменить"). Раскрывается сдвигом .hookah-card-inner. */
.hookah-card-swipe-bg { position: absolute; inset: 0; display: flex; font-weight: 700; font-size: .9rem; }
.swipe-side { flex: 1 1 50%; display: flex; align-items: center; }
.swipe-right-bg { background: var(--good); color: #14260f; justify-content: flex-start; padding-left: 1.2rem; }
.swipe-left-bg { background: var(--bad); color: #2a0f0a; justify-content: flex-end; padding-right: 1.2rem; }
.hookah-card.state-green { border-color: var(--good); }
.hookah-card.state-yellow { border-color: var(--warn); }
.hookah-card.state-red { border-color: var(--bad); }
.hookah-card .h-label { font-size: 1.1rem; font-weight: 700; }
.hookah-card .h-timer { font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.hookah-card .h-meta { font-size: .75rem; color: var(--ink-dim); margin-top: .15rem; }
.hookah-card.status-finished, .hookah-card.status-cancelled, .hookah-card.status-auto_closed {
  opacity: .55; border-color: var(--border);
}
.hookah-card .h-status-badge { font-size: .75rem; color: var(--ink-dim); }

.deliver-row { display: flex; gap: .6rem; margin-bottom: .8rem; }
.deliver-btn {
  flex: 1; padding: 1rem; border: none; border-radius: 10px; font-size: 1rem; font-weight: 700;
}
.deliver-new { background: var(--ember); color: #1a1208; }
.deliver-additional { background: var(--teal); color: #0c211d; }
.replace-coal-btn {
  width: 100%; padding: 1rem; border: none; border-radius: 10px; font-size: 1rem; font-weight: 700;
  background: var(--surface-2); border: 2px solid var(--warn); color: var(--warn);
}

/* --- меню кальяна --- */
.menu-sheet {
  width: 100%; max-width: 420px; background: var(--surface); border-radius: 14px;
  margin-bottom: 2rem; overflow: hidden;
}
.menu-title { padding: .9rem 1rem .4rem; color: var(--ink-dim); font-size: .85rem; }
.menu-item {
  display: block; width: 100%; text-align: center; background: none; border: none; border-top: 1px solid var(--border);
  color: var(--ink); font-size: 1.05rem; padding: 1rem;
}
.menu-item.danger { color: var(--bad); }
.menu-item.cancel { color: var(--ink-dim); }

/* iPad: чуть просторнее сетка */
@media (min-width: 900px) {
  .sheet { max-width: 640px; border-radius: 18px; margin-bottom: 2rem; }
  .sheet-overlay { align-items: center; }
}

/* --- тост после свайпа "скурено" --- */
.toast {
  position: fixed; left: 50%; top: 1.2rem; transform: translate(-50%, -20px);
  background: var(--surface); border: 1px solid var(--good); color: var(--ink);
  padding: .7rem 1.2rem; border-radius: 999px; font-size: .9rem; font-weight: 600;
  box-shadow: 0 4px 16px rgba(0,0,0,.4); opacity: 0; transition: transform .25s ease-out, opacity .25s ease-out;
  z-index: 200; white-space: nowrap; max-width: 90vw; overflow: hidden; text-overflow: ellipsis;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* --- инструкция --- */
.help-content h3 {
  font-family: var(--sans); font-size: .95rem; color: var(--ember); margin: 1.1rem 0 .5rem;
}
.help-content h3:first-child { margin-top: 0; }
.help-content ul { margin: 0; padding-left: 1.2rem; }
.help-content li { font-size: .9rem; line-height: 1.5; color: var(--ink); margin-bottom: .5rem; }
