/* ==========================================================================
   INNODIGYM Power — bright, responsive UI
   Breakpoints: phone < 720px · tablet 720–1023px · laptop ≥ 1024px
   ========================================================================== */

:root {
  color-scheme: light;

  /* surfaces */
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e8f0;
  --border-strong: #cbd5e1;

  /* text */
  --text: #0f172a;
  --text-2: #475569;
  --muted: #64748b;

  /* brand */
  --primary: #0f766e;          /* teal 700 — 5.5:1 on white */
  --primary-hover: #115e59;
  --primary-soft: #e6f6f4;
  --accent: #84cc16;           /* lime, echoes the original brand */
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --ok: #15803d;

  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, .06);
  --shadow: 0 1px 3px rgba(15, 23, 42, .06), 0 8px 24px -8px rgba(15, 23, 42, .10);
  --shadow-lg: 0 24px 48px -12px rgba(15, 23, 42, .25);
  --ring: 0 0 0 3px rgba(15, 118, 110, .25);

  --tabbar-h: 64px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

/* Must win over any ID-selector display rule below, or elements toggled via
   `.hidden = true` stay visible/interactive. The backdrop case is the dangerous
   one: position:fixed + inset:0 would swallow every click on the page. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: Inter, ui-sans-serif, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  background:
    radial-gradient(900px 400px at 85% -10%, rgba(132, 204, 22, .14), transparent 60%),
    radial-gradient(800px 400px at -10% -10%, rgba(15, 118, 110, .12), transparent 60%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 1040px;
  margin: 0 auto;
  padding: 0 24px 48px;
}

/* ---------- header ---------- */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 20px 0 16px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  flex: none;
  width: 40px; height: 40px;
  border-radius: 12px;
  display: grid; place-items: center;
  color: #fff;
  background: linear-gradient(135deg, var(--primary), #14b8a6 60%, var(--accent));
  box-shadow: 0 6px 16px -4px rgba(15, 118, 110, .5);
}
.brand-mark svg { width: 22px; height: 22px; }
header h1 { font-size: 18px; line-height: 1.2; margin: 0; font-weight: 700; letter-spacing: -.01em; }
header h1 span { display: block; color: var(--muted); font-weight: 500; font-size: 12px; letter-spacing: 0; }

#userbar {
  display: flex; align-items: center; gap: 10px;
  color: var(--text-2); font-size: 13px; min-width: 0;
}
#userbar button { margin-left: 0; }

/* ---------- typography ---------- */
h2 { font-size: 20px; line-height: 1.25; letter-spacing: -.01em; color: var(--text); margin: 0; font-weight: 700; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 28px 0 10px; font-weight: 600; }
h3.no-margin { margin: 0; text-transform: none; letter-spacing: 0; font-size: 17px; color: var(--text); }

/* ---------- surfaces ---------- */
section#login-section, .tab-panel, #editor, #list-view {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 28px;
}
/* The Programs tab nests list/editor inside .tab-panel; don't double the card. */
#tab-programs { padding: 0; background: transparent; border: 0; box-shadow: none; }
#main-section { display: flex; flex-direction: column; gap: 16px; }

/* ---------- login ---------- */
section#login-section { max-width: 420px; margin: 7vh auto 0; padding: 32px; }
section#login-section h2 { margin-bottom: 4px; }
.lede { color: var(--text-2); margin: 0 0 20px; }
form#login-form { display: flex; flex-direction: column; gap: 16px; }
form#login-form button { width: 100%; margin-top: 4px; }

/* ---------- forms ---------- */
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 500; color: var(--text-2); }
input[type="email"], input[type="password"], input[type="text"], input[type="number"],
input[type="date"], select {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 12px;
  min-height: 42px;
  border-radius: 10px;
  font: inherit;
  font-size: 15px;
  transition: border-color .15s, box-shadow .15s;
}
input:hover, select:hover { border-color: #94a3b8; }
input:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: var(--ring); }
input:disabled { background: var(--surface-2); color: var(--muted); cursor: not-allowed; }
input[type="number"] { width: 92px; }
input::placeholder { color: #94a3b8; }

/* ---------- buttons ---------- */
button {
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  background: var(--primary);
  color: #fff;
  border: 1px solid transparent;
  padding: 0 16px;
  min-height: 40px;
  border-radius: 10px;
  cursor: pointer;
  transition: background .15s, border-color .15s, box-shadow .15s, transform .05s;
  box-shadow: var(--shadow-sm);
}
button:hover { background: var(--primary-hover); }
button:active { transform: translateY(1px); }
button:focus-visible, a:focus-visible, .tab-btn:focus-visible, .chip:focus-visible, .picker-row:focus-visible,
.cover-thumb-btn:focus-visible, .cover-option:focus-visible {
  outline: none; box-shadow: var(--ring);
}
button.secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border-strong);
  box-shadow: none;
}
button.secondary:hover { background: var(--surface-2); border-color: #94a3b8; }
.danger-btn, button.danger-btn {
  background: var(--surface); color: var(--danger); border-color: #fecaca; box-shadow: none;
}
.danger-btn:hover, button.danger-btn:hover { background: var(--danger-soft); border-color: var(--danger); }
button:disabled { opacity: .5; cursor: not-allowed; }

.error { color: var(--danger); font-size: 13px; }
.hint {
  color: var(--text-2); font-size: 13px; margin: 14px 0;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}

.row-between {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 18px; flex-wrap: wrap;
}
.row-between > div { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- navigation (laptop/tablet: pill row) ---------- */
nav#tabs {
  display: flex; gap: 4px;
  padding: 4px;
  margin-bottom: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-sm);
  overflow-x: auto;
  scrollbar-width: none;
}
nav#tabs::-webkit-scrollbar { display: none; }
nav#tabs .tab-btn {
  flex: 1 0 auto;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: transparent; color: var(--text-2);
  box-shadow: none;
  padding: 0 16px; min-height: 42px;
  border-radius: 10px;
  font-weight: 600; font-size: 14px;
}
nav#tabs .tab-btn svg { width: 18px; height: 18px; flex: none; }
nav#tabs .tab-btn:hover { background: var(--surface-2); color: var(--text); }
nav#tabs .tab-btn.active { background: var(--primary-soft); color: var(--primary); }

/* ---------- list cards ---------- */
#program-list, #actions-list, #history-list, #fitness-list {
  display: flex; flex-direction: column; gap: 10px;
}
.program-card, .record-card, .action-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  transition: border-color .15s, box-shadow .15s;
}
.program-card:hover, .record-card:hover, .action-card:hover { border-color: var(--border-strong); box-shadow: var(--shadow); }
.program-card .name, .record-card .name, .action-card .name {
  font-weight: 600; color: var(--text); overflow-wrap: anywhere;
}
.program-card .meta, .record-card .meta, .action-card .meta {
  font-size: 13px; color: var(--muted); margin-top: 2px; overflow-wrap: anywhere;
}
.card-left { display: flex; align-items: center; gap: 14px; min-width: 0; }
.card-left > div { min-width: 0; }
.card-actions, .program-card > div:last-child:not(.card-left) { display: flex; gap: 8px; flex: none; }

.program-card img.thumb, .action-card img.thumb {
  width: 56px; height: 56px; flex: none;
  object-fit: cover; border-radius: 12px; margin: 0;
  background: var(--surface-2);
}
.empty-state {
  color: var(--muted); font-size: 14px; text-align: center;
  padding: 32px 16px; border: 1px dashed var(--border-strong); border-radius: var(--radius); background: var(--surface-2);
}
.server-error {
  color: var(--danger); font-size: 14px; padding: 12px 14px;
  background: var(--danger-soft); border: 1px solid #fecaca; border-radius: 10px;
}

/* ---------- forms layout ---------- */
.inline-form { display: flex; flex-wrap: wrap; gap: 14px; align-items: flex-end; }
.inline-form.stacked { flex-direction: column; align-items: stretch; max-width: 420px; }
.inline-form label { min-width: 140px; }
.inline-form.stacked input[type="number"] { width: 100%; }
.readonly-field small { color: var(--muted); font-size: 12px; font-weight: 400; }
#save-status, #action-add-status, #profile-status { font-size: 14px; color: var(--muted); }
.ok { color: var(--ok) !important; }
.err { color: var(--danger) !important; }

/* ---------- program editor ---------- */
#editor, #list-view { padding: 28px; }
.editor-top { display: flex; gap: 28px; align-items: flex-start; margin-bottom: 24px; flex-wrap: wrap; }
.name-field { flex: 1 1 280px; max-width: 420px; }
.name-field input { width: 100%; font-size: 16px; }
.cover-field { display: flex; flex-direction: column; gap: 8px; font-size: 13px; font-weight: 500; color: var(--text-2); }
.cover-thumb-btn {
  position: relative; padding: 0; width: 72px; height: 72px; min-height: 0; border-radius: 16px;
  background: var(--surface-2); border: 1px solid var(--border-strong); overflow: hidden; cursor: pointer;
}
.cover-thumb-btn:hover { background: var(--surface-2); transform: scale(1.03); }
.cover-thumb-btn img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: pointer; }
.cover-badge {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 2px 0; text-align: center;
  background: rgba(15, 23, 42, .65); color: #fff; font-size: 11px; font-weight: 600;
}
#cover-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 10px; overflow-y: auto; padding: 4px; }
.cover-option {
  padding: 0; min-height: 0; aspect-ratio: 1; border-radius: 14px; background: var(--surface-2);
  border: 3px solid transparent; overflow: hidden; cursor: pointer;
}
.cover-option:hover { background: var(--surface-2); transform: scale(1.04); }
.cover-option img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: pointer; }
.cover-option.selected { border-color: var(--primary); box-shadow: 0 0 0 2px var(--surface) inset; }

.table-wrap { overflow-x: auto; margin: 0 -4px; }
table#steps-table { width: 100%; border-collapse: collapse; font-size: 14px; }
table#steps-table th {
  text-align: left; color: var(--muted); font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .05em; padding: 8px 8px; border-bottom: 1px solid var(--border);
}
table#steps-table td { padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table#steps-table tbody tr:hover { background: var(--surface-2); }
table#steps-table td.thumb img { width: 44px; height: 44px; object-fit: cover; border-radius: 10px; display: block; }
table#steps-table td.action-name { max-width: 240px; font-weight: 600; }
table#steps-table input[type="number"] { width: 84px; min-height: 38px; padding: 6px 10px; }
table#steps-table td.reorder, table#steps-table th.reorder-col { width: 1%; white-space: nowrap; padding-left: 4px; padding-right: 4px; }
.reorder-btn {
  appearance: none; background: transparent; color: var(--text-2);
  border: 1px solid transparent; border-radius: 8px;
  width: 32px; height: 32px; min-height: 0; padding: 0; margin: 0 1px;
  font-size: 14px; line-height: 1; cursor: pointer; vertical-align: middle;
}
.reorder-btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--border-strong); color: var(--text); }
.reorder-btn:disabled { opacity: .3; cursor: not-allowed; }
.reorder-btn.drag-handle { cursor: grab; touch-action: none; font-size: 18px; user-select: none; -webkit-user-select: none; }
body.reordering, body.reordering * { cursor: grabbing !important; user-select: none; -webkit-user-select: none; }
table#steps-table tr.dragging { opacity: .55; background: var(--surface-2); }
table#steps-table tr.drop-before td { box-shadow: inset 0 3px 0 var(--primary); }
table#steps-table tr.drop-after td { box-shadow: inset 0 -3px 0 var(--primary); }
#add-action-btn { margin-top: 14px; }
.actions { display: flex; align-items: center; gap: 12px; margin-top: 24px; flex-wrap: wrap; }

/* ---------- modals ---------- */
#action-picker-backdrop, #lightbox-backdrop, #cover-modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(15, 23, 42, .5);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
#lightbox-backdrop { z-index: 60; background: rgba(15, 23, 42, .7); }
#action-picker, #lightbox, #cover-modal {
  background: var(--surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
  padding: 20px; display: flex; flex-direction: column; gap: 14px;
  animation: pop .18s ease-out;
}
#action-picker { width: min(560px, 100%); max-height: min(80vh, 720px); }
#lightbox { width: min(600px, 100%); max-height: 90vh; }
#cover-modal { width: min(480px, 100%); max-height: min(80vh, 640px); }
#action-picker > * { flex: none; }
#action-picker > #picker-results { flex: 1 1 auto; min-height: 0; }
#picker-search { width: 100%; }
#picker-chips { display: flex; gap: 6px; flex-wrap: wrap; max-height: 112px; overflow-y: auto; padding: 2px 0; }
.chip {
  background: var(--surface); color: var(--text-2); border: 1px solid var(--border-strong);
  padding: 0 12px; min-height: 32px; border-radius: 999px; font-size: 13px; font-weight: 500; box-shadow: none;
}
.chip:hover { background: var(--surface-2); }
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }
#picker-results { overflow-y: auto; display: flex; flex-direction: column; gap: 4px; margin: 0 -6px; padding: 0 6px; }
.picker-row {
  display: flex; align-items: center; gap: 12px; padding: 8px; border-radius: 12px; cursor: pointer;
  min-height: 56px;
}
.picker-row:hover { background: var(--primary-soft); }
.picker-row img { width: 44px; height: 44px; object-fit: cover; border-radius: 10px; background: var(--surface-2); flex: none; }
.picker-row .meta { font-size: 12px; color: var(--muted); }
#lightbox-media { display: flex; justify-content: center; }
#lightbox-media img, #lightbox-media video { max-width: 100%; max-height: 70vh; border-radius: 12px; }

table#steps-table td.thumb img, .program-card img.thumb, .action-card img.thumb, .picker-row img { cursor: zoom-in; }

/* ---------- toast ---------- */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  z-index: 80;
  background: var(--text); color: #fff;
  padding: 12px 18px; border-radius: 12px; font-size: 14px; max-width: min(480px, calc(100vw - 32px));
  box-shadow: var(--shadow-lg);
  animation: rise .2s ease-out;
}
#toast.err { background: var(--danger); color: #fff !important; }

@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ==========================================================================
   Tablet (720–1023px): tighter gutters
   ========================================================================== */
@media (max-width: 1023px) {
  #app { padding: 0 20px 40px; }
  section#login-section, .tab-panel, #editor, #list-view { padding: 22px; }
}

/* ==========================================================================
   Phone (< 720px): bottom tab bar, stacked cards, sheet-style modals
   ========================================================================== */
@media (max-width: 719px) {
  body { font-size: 16px; }
  #app { padding: 0 16px calc(var(--tabbar-h) + 32px + env(safe-area-inset-bottom)); }
  header { padding: 14px 0 12px; }
  header h1 span { display: none; }
  #userbar #user-email { display: none; }

  section#login-section { margin-top: 4vh; padding: 24px 20px; }
  section#login-section, .tab-panel, #editor, #list-view { padding: 18px 16px; border-radius: var(--radius); }
  h2 { font-size: 18px; }

  /* bottom tab bar */
  nav#tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    margin: 0; padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
    border-radius: 0; border: 0; border-top: 1px solid var(--border);
    background: rgba(255, 255, 255, .92);
    backdrop-filter: saturate(180%) blur(14px);
    box-shadow: 0 -4px 16px rgba(15, 23, 42, .06);
    overflow: visible; gap: 0;
  }
  nav#tabs .tab-btn {
    flex: 1 1 0; min-width: 0;
    flex-direction: column; gap: 2px; padding: 4px 0;
    font-size: 11px; font-weight: 600; border-radius: 12px; min-height: 0;
  }
  nav#tabs .tab-btn svg { width: 22px; height: 22px; }

  /* header controls full-width stacks */
  .row-between { margin-bottom: 14px; }
  .row-between > div { width: 100%; }
  .row-between > div button { flex: 1; }

  /* cards: stack actions under content */
  .program-card { flex-direction: column; align-items: stretch; }
  .program-card .card-actions, .program-card > div:last-child:not(.card-left) { width: 100%; }
  .program-card .card-actions button, .program-card > div:last-child:not(.card-left) button { flex: 1; }
  .record-card, .action-card { align-items: flex-start; }

  .inline-form { flex-direction: column; align-items: stretch; }
  .inline-form label { min-width: 0; }
  .inline-form input[type="number"] { width: 100%; }
  .inline-form button, .inline-form.stacked button { width: 100%; min-height: 46px; }
  .inline-form.stacked { max-width: none; }

  .editor-top { gap: 12px; flex-wrap: nowrap; align-items: flex-end; margin-bottom: 14px; }
  .name-field { max-width: none; flex: 1 1 0; min-width: 0; }
  .cover-thumb-btn { width: 56px; height: 56px; border-radius: 12px; }
  .cover-badge { font-size: 10px; }

  /* steps table → stacked cards: name row, 4 numeric fields, remove */
  .table-wrap { margin: 0; overflow: visible; }
  table#steps-table, table#steps-table tbody { display: block; }
  table#steps-table thead { display: none; }
  table#steps-table tr {
    display: grid; position: relative;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-template-rows: minmax(40px, auto) auto;
    column-gap: 8px; row-gap: 6px; align-items: end;
    padding: 8px 10px 10px 34px; margin-bottom: 8px;
    border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2);
  }
  table#steps-table td { display: block; padding: 0; border: 0; }
  table#steps-table td.thumb { grid-row: 1; grid-column: 1; align-self: center; }
  table#steps-table td.thumb img { width: 40px; height: 40px; }
  table#steps-table td.num { display: none; }
  table#steps-table td.action-name { grid-row: 1; grid-column: 2 / 5; max-width: none; align-self: center; padding-right: 80px; font-size: 14px; line-height: 1.25; }
  table#steps-table td.field { display: flex; flex-direction: column; gap: 2px; grid-row: 2; }
  table#steps-table td.field::before {
    content: attr(data-label); font-size: 10px; font-weight: 600; color: var(--muted);
    text-transform: uppercase; letter-spacing: .04em;
  }
  table#steps-table td.field input { width: 100%; min-height: 38px; padding: 4px 8px; }
  /* icon-only actions, top-right of the card */
  table#steps-table td.row-actions { position: absolute; top: 6px; right: 8px; display: flex; gap: 4px; }
  table#steps-table td.row-actions button { flex: none; width: 32px; min-height: 32px; height: 32px; padding: 0; display: inline-flex; align-items: center; justify-content: center; }
  table#steps-table .row-actions .lbl { display: none; }
  table#steps-table .row-actions .ico { display: inline-flex; }
  /* drag handle as a slim strip on the left; up/down hidden on mobile */
  table#steps-table td.reorder { position: absolute; left: 0; top: 0; bottom: 0; width: 30px; display: flex; align-items: center; justify-content: center; padding: 0; }
  table#steps-table .reorder-btn[data-reorder="up"], table#steps-table .reorder-btn[data-reorder="down"] { display: none; }
  table#steps-table .reorder-btn.drag-handle { width: 26px; height: 100%; min-height: 0; border: 0; background: transparent; box-shadow: none; padding: 0; color: var(--muted); border-radius: var(--radius) 0 0 var(--radius); }
  table#steps-table tr.drop-before { box-shadow: 0 -3px 0 var(--primary); }
  table#steps-table tr.drop-after { box-shadow: 0 3px 0 var(--primary); }
  table#steps-table tr.drop-before td, table#steps-table tr.drop-after td { box-shadow: none; }
  #add-action-btn { width: 100%; min-height: 46px; }
  .actions button { flex: 1; min-height: 46px; }

  /* modals become bottom sheets */
  #action-picker-backdrop, #lightbox-backdrop, #cover-modal-backdrop { align-items: flex-end; padding: 0; }
  #action-picker, #lightbox, #cover-modal {
    width: 100%; max-height: 88vh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
    animation: sheet .22s ease-out;
  }
  #action-picker { height: 88vh; }
  #toast { bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom)); }
}
@keyframes sheet { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }

/* duplicate-step actions + new-row highlight */
.row-actions { white-space: nowrap; }
.row-actions .ico { display: none; }
.row-actions button { margin-right: 6px; }
.row-actions button:last-child { margin-right: 0; }
@keyframes row-new-flash { from { background: var(--primary-soft); box-shadow: inset 3px 0 0 var(--primary); } to { background: transparent; box-shadow: inset 3px 0 0 transparent; } }
tr.row-new { animation: row-new-flash 1.4s ease-out; }
@media (max-width: 719px) { .row-actions button { margin-right: 0; } }

/* ---------- touch + motion ---------- */
@media (pointer: coarse) {
  button, input, select { min-height: 44px; }
  .chip { min-height: 36px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
