/* Modal Kit — shared modal shell + form atoms.
   1:1 port of _design/zain-os/project/Modal Kit.html.
   All classes scoped with `mk-` prefix so they don't collide with the
   existing legacy modal CSS in styles.css. Use these on new modals and
   progressively migrate old ones. */

/* ---------- Overlay + shell ---------- */
.mk-overlay { position:fixed; inset:0; background: rgba(20,18,17,0.65); backdrop-filter: blur(4px); display:flex; align-items:safe center; justify-content:center; padding:40px 20px; z-index:100; animation: mk-fade-in 160ms ease; overflow-y:auto; }
@keyframes mk-fade-in { from { opacity:0; } to { opacity:1; } }

.mk-modal { width:560px; max-width:100%; max-height: calc(100vh - 100px); background:var(--card); border:1px solid var(--border); border-radius:var(--r-lg); box-shadow: 0 20px 60px rgba(0,0,0,0.6); overflow:hidden; display:flex; flex-direction:column; animation: mk-modal-in 200ms cubic-bezier(0.2,0,0,1); }
.mk-modal.mk-wide { width:720px; }
@keyframes mk-modal-in { from { opacity:0; transform: translateY(16px); } to { opacity:1; transform: translateY(0); } }

.mk-head { padding:18px 22px 16px; border-bottom:1px solid var(--border-light); display:flex; align-items:flex-start; gap:14px; }
.mk-head .h-title { flex:1; }
.mk-head h3 { margin:0 0 3px; font-size:15px; font-weight:600; letter-spacing:-0.005em; }
.mk-head .sub { font-size:12px; color:var(--text-secondary); }
.mk-close-x { width:28px; height:28px; border-radius:6px; border:1px solid var(--border-light); background:transparent; color:var(--text-secondary); cursor:pointer; display:inline-flex; align-items:center; justify-content:center; }
.mk-close-x:hover { color:var(--text); border-color:var(--border); }
.mk-close-x svg { width:12px; height:12px; }

.mk-body { padding:18px 22px 4px; overflow-y:auto; flex:1; }
.mk-foot { display:flex; align-items:center; justify-content:space-between; gap:12px; padding:14px 22px; border-top:1px solid var(--border-light); background: rgba(20,18,17,0.4); }
.mk-foot-left { font-size:11.5px; color:var(--text-tertiary); font-family:var(--font-mono); letter-spacing:0.04em; }
.mk-foot-right { display:flex; gap:8px; }

/* ---------- Buttons ---------- */
.mk-btn { height:32px; padding:0 14px; background:var(--card); border:1px solid var(--border); border-radius:var(--r-sm); color:var(--text); font-size:12.5px; font-weight:500; cursor:pointer; font-family:inherit; display:inline-flex; align-items:center; gap:6px; }
.mk-btn:hover { border-color:var(--brand-dim); color:var(--brand); }
.mk-btn:disabled { opacity: 0.5; pointer-events: none; }
.mk-btn.mk-primary { background:var(--brand); border-color:var(--brand); color:#fff; font-weight:600; }
.mk-btn.mk-primary:hover { background:var(--brand-hover); border-color:var(--brand-hover); color:#fff; }
.mk-btn.mk-ghost { border-color:transparent; background:transparent; color:var(--text-secondary); }
.mk-btn.mk-ghost:hover { background:var(--hover); color:var(--text); }
.mk-btn.mk-danger { color:#C8553D; border-color: rgba(200,85,61,0.3); }
.mk-btn.mk-danger:hover { color:#fff; background:#C8553D; border-color:#C8553D; }
.mk-btn svg { width:12px; height:12px; }

/* ---------- Form atoms ---------- */
.mk-section-title { font-size:10.5px; font-family:var(--font-mono); text-transform:uppercase; letter-spacing:0.08em; color:var(--text-tertiary); font-weight:500; margin:18px 0 10px; padding-bottom:6px; border-bottom:1px dashed var(--border-light); }
.mk-section-title:first-child { margin-top:4px; }

.mk-row { display:grid; grid-template-columns: 1fr 1fr; gap:14px; margin-bottom:12px; }
.mk-row.mk-single { grid-template-columns: 1fr; }
.mk-row.mk-three { grid-template-columns: 1fr 1fr 1fr; }

.mk-field { display:flex; flex-direction:column; gap:5px; }
.mk-field label { font-size:10.5px; font-family:var(--font-mono); text-transform:uppercase; letter-spacing:0.06em; color:var(--text-secondary); font-weight:500; }
.mk-field .mk-req { color:var(--brand); margin-left:2px; }
.mk-field input, .mk-field textarea, .mk-field select {
  height:34px; padding:0 10px; background: var(--bg); border:1px solid var(--border-light);
  border-radius:var(--r-sm); color:var(--text); font-size:13px; font-family:inherit;
  box-sizing: border-box; outline:0;
}
.mk-field textarea { height:auto; padding:8px 10px; resize:vertical; min-height:64px; line-height:1.4; }
.mk-field input:focus, .mk-field textarea:focus, .mk-field select:focus {
  outline:0; border-color:var(--brand-dim); background: rgba(232,97,58,0.03);
}
.mk-field input::placeholder, .mk-field textarea::placeholder { color:var(--text-tertiary); }
.mk-field .mk-hint { font-size:10.5px; color:var(--text-tertiary); font-family:var(--font-mono); letter-spacing:0.02em; }
.mk-field.mk-has-prefix { position:relative; }
.mk-field .mk-prefix { position:absolute; left:10px; bottom:9px; font-size:12px; color:var(--text-tertiary); font-family:var(--font-mono); pointer-events:none; }
.mk-field.mk-has-prefix input { padding-left:34px; }

/* Chip group */
.mk-chip-group { display:flex; flex-wrap:wrap; gap:6px; }
.mk-chip { padding:5px 12px; background: transparent; border:1px solid var(--border-light); border-radius:var(--r-pill); font-size:11.5px; color:var(--text-secondary); cursor:pointer; font-family:inherit; display:inline-flex; align-items:center; gap:8px; }
.mk-chip:hover { border-color:var(--brand-dim); color:var(--text); }
.mk-chip.mk-on { background:var(--brand-soft); border-color:var(--brand); color:var(--brand); font-weight:500; }
.mk-chip .mk-chip-label { display:inline-block; }

/* Secondary tier: dimmer fill, dashed border so it's distinguishable from primary at a glance. */
.mk-chip.mk-on.mk-tier-s { background: rgba(232,97,58,0.06); border-style: dashed; color: var(--text-secondary); font-weight:400; }

/* Inline P|S 2-segment pill, lives inside an .mk-chip when it is .mk-on. */
.mk-chip-tier { display:inline-flex; align-items:center; border:1px solid var(--border-light); border-radius:var(--r-pill); overflow:hidden; height:16px; line-height:14px; font-family:var(--font-mono); font-size:9.5px; font-weight:600; letter-spacing:0.04em; }
.mk-chip-tier .mk-tier-seg { padding:0 6px; height:100%; display:inline-flex; align-items:center; cursor:pointer; color:var(--text-tertiary); background:transparent; }
.mk-chip-tier .mk-tier-seg + .mk-tier-seg { border-left:1px solid var(--border-light); }
.mk-chip-tier .mk-tier-seg:hover { color:var(--text); }
.mk-chip-tier .mk-tier-seg.mk-on { background:var(--brand); color:#fff; }

.mk-field-section-label { font-size:10.5px; font-family:var(--font-mono); text-transform:uppercase; letter-spacing:0.06em; color:var(--text-secondary); font-weight:500; margin-bottom:6px; }

/* Toggle row */
.mk-toggle-row { display:flex; align-items:center; gap:12px; padding:10px 12px; background:var(--bg); border:1px solid var(--border-light); border-radius:var(--r-sm); margin-bottom:12px; }
.mk-toggle-row .tg-text { flex:1; }
.mk-toggle-row .tg-title { font-size:12.5px; font-weight:500; }
.mk-toggle-row .tg-desc { font-size:11px; color:var(--text-tertiary); margin-top:2px; }
.mk-tg { width:32px; height:18px; background: var(--border); border-radius:var(--r-pill); position:relative; flex-shrink:0; cursor:pointer; transition: background 160ms; border:0; padding:0; }
.mk-tg::after { content:''; position:absolute; top:2px; left:2px; width:14px; height:14px; border-radius:50%; background: var(--text); transition: transform 160ms; }
.mk-tg.mk-on { background: var(--brand); }
.mk-tg.mk-on::after { transform: translateX(14px); background: #fff; }

/* Upload dropzone */
.mk-dz { border:1.5px dashed var(--border); border-radius:var(--r-md); padding:28px 18px; text-align:center; background:var(--bg); transition: border-color 160ms, background 160ms; cursor:pointer; }
.mk-dz:hover, .mk-dz.mk-dz-hot { border-color:var(--brand); background: rgba(232,97,58,0.06); }
.mk-dz .mk-dz-icon { width:36px; height:36px; margin:0 auto 10px; border-radius:50%; background:var(--brand-soft); display:flex; align-items:center; justify-content:center; color:var(--brand); }
.mk-dz .mk-dz-icon svg { width:16px; height:16px; }
.mk-dz .mk-dz-title { font-size:13px; font-weight:500; margin-bottom:4px; }
.mk-dz .mk-dz-sub { font-size:11.5px; color:var(--text-tertiary); }
.mk-dz .mk-dz-sub em { color:var(--brand); font-style:normal; text-decoration:underline; cursor:pointer; }

/* Preview list (for imported installments) */
.mk-preview-list { border:1px solid var(--border-light); border-radius:var(--r-sm); overflow:hidden; background:var(--bg); }
.mk-pl-head, .mk-pl-row { display:grid; grid-template-columns: 40px 1fr 1fr 1fr 24px; gap:10px; padding:8px 12px; align-items:center; font-size:11.5px; }
.mk-pl-head { background: rgba(20,18,17,0.6); border-bottom:1px solid var(--border-light); font-family:var(--font-mono); text-transform:uppercase; letter-spacing:0.06em; font-size:10px; color:var(--text-tertiary); font-weight:500; }
.mk-pl-row { border-bottom:1px solid var(--border-light); }
.mk-pl-row:last-child { border-bottom:0; }
.mk-pl-num { font-family:var(--font-mono); color:var(--text-tertiary); font-variant-numeric:tabular-nums; }
.mk-pl-row input { width:100%; height:26px; padding:0 8px; background:transparent; border:1px solid transparent; border-radius:3px; color:var(--text); font-size:12px; font-family:inherit; font-variant-numeric:tabular-nums; box-sizing:border-box; outline:0; }
.mk-pl-row input:hover { border-color:var(--border-light); }
.mk-pl-row input:focus { outline:0; border-color:var(--brand-dim); background:var(--card); }
.mk-pl-del { width:20px; height:20px; border-radius:3px; background:transparent; border:0; color:var(--text-tertiary); cursor:pointer; display:inline-flex; align-items:center; justify-content:center; }
.mk-pl-del:hover { color:#C8553D; background: rgba(200,85,61,0.08); }
.mk-pl-del svg { width:11px; height:11px; }

/* Per-row status indicator (sits next to .mk-pl-num) */
.mk-pl-status { display:inline-block; margin-left:6px; font-family:var(--font-mono); font-size:10px; font-weight:600; line-height:14px; padding:0 5px; border-radius:var(--r-pill); }
.mk-pl-status.pending { background: rgba(232,97,58,0.12); color: var(--brand); }
.mk-pl-status.ok { background: rgba(106,138,112,0.18); color:#A3C0A8; }
.mk-pl-status.err { background: rgba(200,85,61,0.18); color:#E08877; }

/* Picker rows (unit/client selector) */
.mk-picker { display:flex; align-items:center; gap:10px; padding:8px 10px; background:var(--bg); border:1px solid var(--border-light); border-radius:var(--r-sm); cursor:pointer; }
.mk-picker:hover { border-color:var(--brand-dim); }
.mk-picker .p-icon { width:28px; height:28px; border-radius:6px; background:var(--brand-soft); color:var(--brand); display:flex; align-items:center; justify-content:center; font-size:11px; font-family:var(--font-mono); font-weight:600; flex-shrink:0; }
.mk-picker .p-main { flex:1; min-width:0; }
.mk-picker .p-title { font-size:12.5px; font-weight:500; }
.mk-picker .p-sub { font-size:10.5px; color:var(--text-tertiary); font-family:var(--font-mono); letter-spacing:0.02em; margin-top:1px; }
.mk-picker .p-chev { color:var(--text-tertiary); font-size:11px; }

/* Time slot picker (visit modal) */
.mk-slots { display:grid; grid-template-columns: repeat(4, 1fr); gap:6px; }
.mk-slot { padding:8px 6px; text-align:center; background:var(--bg); border:1px solid var(--border-light); border-radius:var(--r-sm); font-size:11.5px; font-family:var(--font-mono); font-variant-numeric:tabular-nums; cursor:pointer; color:var(--text-secondary); }
.mk-slot:hover { border-color:var(--brand-dim); color:var(--text); }
.mk-slot.mk-on { background:var(--brand-soft); border-color:var(--brand); color:var(--brand); font-weight:600; }
.mk-slot.mk-off { opacity:0.35; cursor:not-allowed; text-decoration:line-through; }

/* Photo grid mini */
.mk-photo-mini { display:grid; grid-template-columns: repeat(4, 1fr); gap:6px; }
.mk-photo-cell { aspect-ratio:1; background: linear-gradient(135deg, rgba(232,97,58,0.12), rgba(232,97,58,0.04)); border-radius:var(--r-sm); border:1px solid var(--border-light); display:flex; align-items:center; justify-content:center; color:var(--text-tertiary); font-size:10px; font-family:var(--font-mono); position:relative; overflow:hidden; background-size:cover; background-position:center; }
.mk-photo-cell.mk-add { border-style:dashed; background:var(--bg); color:var(--brand); cursor:pointer; }
.mk-photo-cell.mk-add:hover { border-color:var(--brand-dim); background: rgba(232,97,58,0.03); }

/* Status strip (for async jobs like PDF extract) */
.mk-status-strip { display:flex; align-items:center; gap:12px; padding:10px 14px; background: rgba(232,97,58,0.06); border:1px solid rgba(232,97,58,0.2); border-radius:var(--r-sm); margin-bottom:14px; font-size:12px; }
.mk-status-strip .dot { width:8px; height:8px; border-radius:50%; background:var(--brand); flex-shrink:0; animation: mk-pulse 1.4s ease-in-out infinite; }
@keyframes mk-pulse { 0%,100% { opacity:0.4; } 50% { opacity:1; } }
.mk-status-strip .s-main { flex:1; }
.mk-status-strip .s-title { font-weight:500; }
.mk-status-strip .s-sub { font-size:10.5px; color:var(--text-secondary); font-family:var(--font-mono); margin-top:1px; }
.mk-status-strip .s-count { font-size:11px; font-family:var(--font-mono); color:var(--brand); font-weight:600; }

/* ==========================================================================
   Responsive — full-screen sheet pattern on phones, comfortable padding on
   tablets. Desktop (>1024px) is untouched.
   ========================================================================== */
@media (max-width: 1024px) {
  .mk-overlay { padding: 24px 14px; }
  .mk-modal, .mk-modal.mk-wide { width: 100%; max-width: 640px; max-height: calc(100vh - 48px); }
  .mk-row.mk-three { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 640px) {
  /* Edge-to-edge sheet, no padding around the overlay */
  .mk-overlay { padding: 0; align-items: stretch; justify-content: stretch; }
  .mk-modal, .mk-modal.mk-wide {
    width: 100%;
    max-width: 100%;
    max-height: 100vh;
    height: 100vh;
    border-radius: 0;
    border-left: 0;
    border-right: 0;
  }
  .mk-head { padding: 14px 16px 12px; position: sticky; top: 0; background: var(--card); z-index: 2; }
  .mk-head h3 { font-size: 16px; }
  .mk-body { padding: 14px 16px 12px; }
  .mk-foot {
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    flex-wrap: wrap;
    gap: 10px;
    position: sticky;
    bottom: 0;
    z-index: 2;
  }
  .mk-foot-left { order: 2; width: 100%; text-align: center; }
  .mk-foot-right { order: 1; width: 100%; justify-content: stretch; }
  .mk-foot-right .mk-btn { flex: 1 1 auto; justify-content: center; height: 40px; }

  /* All rows stack on phones */
  .mk-row, .mk-row.mk-three { grid-template-columns: 1fr; gap: 10px; }
  .mk-row { margin-bottom: 10px; }

  /* iOS won't zoom on focus when input font-size ≥ 16px */
  .mk-field input,
  .mk-field textarea,
  .mk-field select { font-size: 16px; height: 40px; }
  .mk-field textarea { min-height: 80px; height: auto; }

  .mk-section-title { margin: 14px 0 8px; }
  .mk-photo-mini { grid-template-columns: repeat(3, 1fr); }
}

/* Larger tap targets on touch */
@media (hover: none) and (pointer: coarse) {
  .mk-btn { min-height: 36px; }
  .mk-close-x { width: 32px; height: 32px; }
  .mk-chip { min-height: 32px; padding: 6px 12px; }
  .mk-slot { min-height: 36px; }
  .mk-tg { min-width: 32px; min-height: 18px; }
  .mk-pl-del { min-width: 28px; min-height: 28px; }
}

/* ============================================================
   Phone refinements — additive on top of the 640px block above
   (kept separate so it stays easy to read what each rule does).
   ============================================================ */
@media (max-width: 640px) {
  /* Section titles use slightly bigger leading on phone */
  .mk-section-title { font-size: 11px; padding-bottom: 8px; }

  /* Labels readable */
  .mk-field label { font-size: 11px; }
  .mk-field .mk-hint { font-size: 11px; }

  /* Toggle row: bigger title + descriptions tap surface */
  .mk-toggle-row { padding: 12px 12px; gap: 14px; }
  .mk-toggle-row .tg-title { font-size: 14px; }
  .mk-toggle-row .tg-desc { font-size: 12px; }
  .mk-tg { width: 38px; height: 22px; }
  .mk-tg::after { width: 18px; height: 18px; }
  .mk-tg.mk-on::after { transform: translateX(16px); }

  /* Chips: large enough to tap */
  .mk-chip { padding: 7px 14px; font-size: var(--fs-13); min-height: 34px; }
  .mk-chip-tier { height: 18px; line-height: 16px; }

  /* Time slot picker: 3 cols on phone (was 4) so each slot is wider */
  .mk-slots { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .mk-slot { padding: 10px 6px; font-size: var(--fs-13); }

  /* Preview list — horizontal scroll wrapper since 5-column grid
     can't comfortably fit at 375px */
  .mk-preview-list { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .mk-pl-head, .mk-pl-row { min-width: 480px; }
  .mk-pl-row input { font-size: 16px; height: 32px; }

  /* Status strip stacks comfortably */
  .mk-status-strip { padding: 12px 14px; }
  .mk-status-strip .s-title { font-size: var(--fs-13); }

  /* Picker rows: larger icon + title for tap */
  .mk-picker { padding: 12px 12px; min-height: 52px; }
  .mk-picker .p-icon { width: 32px; height: 32px; }
  .mk-picker .p-title { font-size: var(--fs-14); }
  .mk-picker .p-sub { font-size: 11px; }

  /* Dropzone gets more vertical room on phone */
  .mk-dz { padding: 32px 16px; }
  .mk-dz .mk-dz-icon { width: 40px; height: 40px; }
  .mk-dz .mk-dz-title { font-size: var(--fs-14); }

  /* Footer button row: stack with primary on top, full-width.
     The 640 block above already makes them flex:1 with height:40px;
     this stacks them vertically so the primary action lives above
     the cancel action (matches platform convention). */
  .mk-foot-right { flex-direction: column-reverse; gap: 8px; }
  .mk-foot-right .mk-btn { width: 100%; flex: 0 0 auto; }
}

/* Extra-narrow phones (iPhone SE ≈ 320px) */
@media (max-width: 360px) {
  .mk-head { padding: 12px 14px 10px; }
  .mk-body { padding: 12px 14px; }
  .mk-foot { padding: 10px 14px calc(10px + env(safe-area-inset-bottom)); }
  .mk-slots { grid-template-columns: repeat(2, 1fr); }
  .mk-photo-mini { grid-template-columns: repeat(2, 1fr); }
}
