/* ============================================================
   PCIT Service SMS Gateway — Stylesheet
   ============================================================ */

:root {
    --primary:      #2563eb;
    --primary-dark: #1d4ed8;
    --primary-bg:   #eff6ff;
    --orange:       #d97706;
    --orange-bg:    #fffbeb;
    --green:        #059669;
    --green-bg:     #ecfdf5;
    --gray:         #6b7280;
    --gray-bg:      #f9fafb;
    --purple:       #7c3aed;
    --red:          #dc2626;
    --red-bg:       #fef2f2;

    --bg:           #f1f5f9;
    --surface:      #ffffff;
    --border:       #e2e8f0;
    --text:         #1e293b;
    --text-muted:   #64748b;
    --shadow:       0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md:    0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -1px rgba(0,0,0,.06);
    --radius:       10px;
    --radius-sm:    6px;
    --font:         'Inter', system-ui, -apple-system, sans-serif;
    --header-h:     62px;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 15px; }
body {
    font-family: var(--font);
    background:  var(--bg);
    color:       var(--text);
    min-height:  100vh;
}
a { color: var(--primary); text-decoration: none; }
button { font-family: var(--font); cursor: pointer; }
input, select, textarea { font-family: var(--font); font-size: .9rem; }

/* ── Header ── */
.header {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: var(--header-h);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 1.25rem;
    gap: 1rem;
    z-index: 100;
    box-shadow: var(--shadow);
}

.logo {
    display: flex;
    align-items: center;
    gap: .6rem;
    min-width: 160px;
}
.logo-icon { font-size: 1.6rem; }
.logo-title { font-size: .95rem; font-weight: 700; color: var(--text); line-height: 1.2; }
.logo-sub   { font-size: .72rem; color: var(--text-muted); }

/* ── Nav tabs ── */
.nav-tabs {
    display: flex;
    gap: .25rem;
    flex: 1;
    justify-content: center;
}
.nav-tab {
    display: flex;
    align-items: center;
    gap: .4rem;
    padding: .45rem .85rem;
    border: none;
    background: transparent;
    color: var(--text-muted);
    border-radius: var(--radius-sm);
    font-size: .88rem;
    font-weight: 500;
    transition: all .15s;
}
.nav-tab:hover  { background: var(--bg); color: var(--text); }
.nav-tab.active { background: var(--primary-bg); color: var(--primary); font-weight: 600; }

/* ── Connection status ── */
.connection-status {
    display: flex;
    align-items: center;
    gap: .4rem;
    font-size: .8rem;
    color: var(--text-muted);
    min-width: 110px;
    justify-content: flex-end;
}
.status-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: var(--gray);
    flex-shrink: 0;
    transition: background .3s;
}
.connection-status.online  .status-dot { background: var(--green); }
.connection-status.offline .status-dot { background: var(--red); }
.connection-status.online  .status-label { color: var(--green); }
.connection-status.offline .status-label { color: var(--red); }

/* ── Main layout ── */
.main {
    margin-top: var(--header-h);
    padding: 1.25rem;
    max-width: 1280px;
    margin-left: auto;
    margin-right: auto;
}

/* ── Stats bar ── */
.stats-bar {
    display: flex;
    gap: .75rem;
    margin-bottom: 1.25rem;
}
.stat-card {
    flex: 1;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: .9rem 1rem;
    text-align: center;
    box-shadow: var(--shadow);
}
.stat-num {
    font-size: 1.6rem;
    font-weight: 700;
    color: var(--text);
    line-height: 1;
    margin-bottom: .25rem;
}
.stat-num.blue   { color: var(--primary); }
.stat-num.orange { color: var(--orange); }
.stat-num.green  { color: var(--green); }
.stat-num.gray   { color: var(--gray); }
.stat-num.purple { color: var(--purple); }
.stat-lbl {
    font-size: .72rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: .04em;
    font-weight: 500;
}

/* ── Tabs ── */
.tab-pane { display: none; }
.tab-pane.active { display: block; }

/* ── Card ── */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}
.card + .card { margin-top: 1rem; }

.card-header {
    padding: 1.1rem 1.25rem .5rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 1.1rem;
}
.card-header h2 { font-size: 1.05rem; font-weight: 700; margin-bottom: .2rem; }
.card-header h3 { font-size: .95rem; font-weight: 600; }
.card-header p  { font-size: .83rem; color: var(--text-muted); margin-top: .2rem; }

.card-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
    gap: .75rem;
}
.card-toolbar h2 { font-size: 1.05rem; font-weight: 700; }
.toolbar-right { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }

/* ── Two-col layout ── */
.two-col {
    display: grid;
    grid-template-columns: 1fr 380px;
    gap: 1.25rem;
    align-items: start;
}
.form-card { padding: 0; }
.form-card .card-header { margin-bottom: 0; }
.form-card .form-group,
.form-card .btn-large {
    padding-left: 1.25rem;
    padding-right: 1.25rem;
}
.form-card .form-group:first-of-type { padding-top: 1.1rem; }
.form-card .btn-large { margin: 1.1rem 1.25rem 1.25rem; width: calc(100% - 2.5rem); }

.side-col { display: flex; flex-direction: column; gap: 1rem; }

/* ── Forms ── */
.form-group { margin-bottom: .9rem; }
.form-group label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: .83rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: .35rem;
}
.req { color: var(--red); }

.row-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: .75rem;
}
.row-2 .form-group { margin-bottom: 0; }

input[type="text"],
input[type="tel"],
input[type="search"],
select,
textarea {
    width: 100%;
    padding: .5rem .75rem;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: .9rem;
    transition: border-color .15s, box-shadow .15s;
    outline: none;
}
input:focus, select:focus, textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}
textarea { resize: vertical; }
input[type="search"] { padding-left: .75rem; }

.char-count {
    font-size: .78rem;
    font-weight: 400;
    color: var(--text-muted);
}
.char-count.warn  { color: var(--orange); }
.char-count.over  { color: var(--red); }

.msg-hint {
    font-size: .78rem;
    color: var(--text-muted);
    margin-top: .3rem;
    line-height: 1.4;
}
.msg-hint code {
    background: var(--bg);
    padding: .1rem .3rem;
    border-radius: 3px;
    font-size: .8em;
}

/* ── Status buttons ── */
.status-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: .5rem;
}
.status-btn {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .65rem .9rem;
    border: 2px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text-muted);
    font-size: .88rem;
    font-weight: 500;
    transition: all .15s;
    text-align: left;
    cursor: pointer;
}
.status-btn:hover {
    border-color: currentColor;
    opacity: .85;
}
.status-btn.selected {
    border-color: currentColor;
    font-weight: 600;
}
.status-btn[data-status="received"]    { --c: var(--primary); }
.status-btn[data-status="in_progress"] { --c: var(--orange);  }
.status-btn[data-status="ready"]       { --c: var(--green);   }
.status-btn[data-status="completed"]   { --c: var(--gray);    }

.status-btn.selected { color: var(--c); background: color-mix(in srgb, var(--c) 8%, white); border-color: var(--c); }

/* ── Checkbox ── */
.checkbox-row { display: flex; align-items: center; }
.checkbox-label {
    display: flex;
    align-items: center;
    gap: .5rem;
    cursor: pointer;
    font-size: .88rem;
    font-weight: normal !important;
}
.checkbox-label input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--primary); }

/* ── Buttons ── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .4rem;
    padding: .5rem 1rem;
    border-radius: var(--radius-sm);
    font-size: .88rem;
    font-weight: 600;
    border: 1.5px solid transparent;
    transition: all .15s;
    white-space: nowrap;
    cursor: pointer;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); border-color: var(--primary-dark); }

.btn-outline {
    background: transparent;
    color: var(--text-muted);
    border-color: var(--border);
}
.btn-outline:hover:not(:disabled) { background: var(--bg); color: var(--text); }

.btn-danger {
    background: transparent;
    color: var(--red);
    border-color: transparent;
}
.btn-danger:hover:not(:disabled) { background: var(--red-bg); }

.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--text-muted);
    padding: .35rem .6rem;
    font-size: .82rem;
}
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-large {
    display: flex;
    width: 100%;
    padding: .75rem 1rem;
    font-size: 1rem;
    border-radius: var(--radius-sm);
}

/* ── SMS Bubble (preview) ── */
.preview-card { padding: 1.1rem 1.25rem 1.25rem; }
.preview-card .card-header { padding: 0 0 .75rem; border-bottom: none; margin-bottom: .75rem; }
.sms-bubble-wrap {
    background: var(--bg);
    border-radius: var(--radius-sm);
    padding: 1rem;
    min-height: 90px;
}
.sms-bubble {
    background: var(--primary);
    color: white;
    border-radius: 16px 16px 4px 16px;
    padding: .8rem 1rem;
    font-size: .88rem;
    line-height: 1.55;
    max-width: 90%;
    word-break: break-word;
    min-height: 2.5rem;
}
.sms-bubble.empty {
    background: var(--surface);
    color: var(--text-muted);
    border: 1.5px dashed var(--border);
    font-style: italic;
}
.preview-meta {
    margin-top: .6rem;
    font-size: .78rem;
    color: var(--text-muted);
}

/* ── Quick tips card ── */
.quick-card { padding: 1.1rem 1.25rem 1.25rem; }
.quick-card .card-header { padding: 0 0 .75rem; border-bottom: none; margin-bottom: .75rem; }
.tips-list { padding-left: 1.2rem; display: flex; flex-direction: column; gap: .5rem; }
.tips-list li { font-size: .84rem; color: var(--text-muted); line-height: 1.45; }

/* ── Repairs list ── */
.repairs-table {
    width: 100%;
    border-collapse: collapse;
}
.repairs-table th {
    text-align: left;
    padding: .6rem 1rem;
    font-size: .75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--text-muted);
    background: var(--bg);
    border-bottom: 1px solid var(--border);
}
.repairs-table td {
    padding: .75rem 1rem;
    font-size: .88rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.repairs-table tr:last-child td { border-bottom: none; }
.repairs-table tr:hover td { background: #fafbfd; }

/* ── Status badge ── */
.badge {
    display: inline-flex;
    align-items: center;
    gap: .3rem;
    padding: .2rem .6rem;
    border-radius: 20px;
    font-size: .78rem;
    font-weight: 600;
    white-space: nowrap;
}
.badge-received    { background: var(--primary-bg); color: var(--primary); }
.badge-in_progress { background: var(--orange-bg);  color: var(--orange);  }
.badge-ready       { background: var(--green-bg);   color: var(--green);   }
.badge-completed   { background: var(--gray-bg);    color: var(--gray);    }

/* ── Status select (in table) ── */
.status-select {
    width: auto;
    padding: .25rem .5rem;
    font-size: .82rem;
    border-radius: 4px;
}

/* ── SMS History ── */
.history-list { list-style: none; }
.history-item {
    display: flex;
    align-items: flex-start;
    gap: .75rem;
    padding: .85rem 1.25rem;
    border-bottom: 1px solid var(--border);
    transition: background .1s;
}
.history-item:last-child { border-bottom: none; }
.history-item:hover { background: #fafbfd; }

.history-icon {
    width: 32px; height: 32px;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: .95rem;
    flex-shrink: 0;
}
.history-icon.sent   { background: var(--green-bg); }
.history-icon.failed { background: var(--red-bg); }

.history-body { flex: 1; min-width: 0; }
.history-meta {
    display: flex;
    align-items: center;
    gap: .5rem;
    flex-wrap: wrap;
    margin-bottom: .2rem;
}
.history-name  { font-weight: 600; font-size: .88rem; }
.history-phone { font-size: .8rem; color: var(--text-muted); }
.history-time  { font-size: .77rem; color: var(--text-muted); margin-left: auto; }
.history-msg   { font-size: .83rem; color: var(--text-muted); line-height: 1.45; }
.history-status-dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    flex-shrink: 0;
}
.history-status-dot.sent   { background: var(--green); }
.history-status-dot.failed { background: var(--red); }

/* ── Settings ── */
.settings-msg {
    margin-top: .75rem;
    padding: .65rem .9rem;
    border-radius: var(--radius-sm);
    font-size: .85rem;
}
.settings-msg.ok  { background: var(--green-bg); color: var(--green); }
.settings-msg.err { background: var(--red-bg);   color: var(--red);   }

.setup-steps { padding: 1.1rem 1.25rem 1.25rem; display: flex; flex-direction: column; gap: 1rem; }
.step { display: flex; align-items: flex-start; gap: .75rem; }
.step-num {
    width: 24px; height: 24px;
    border-radius: 50%;
    background: var(--primary);
    color: white;
    font-size: .78rem;
    font-weight: 700;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.step { font-size: .85rem; color: var(--text-muted); line-height: 1.55; }
.step strong { color: var(--text); }
.step code {
    display: block;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: .25rem .5rem;
    margin-top: .25rem;
    font-size: .82rem;
    font-family: 'Menlo', 'Consolas', monospace;
    color: var(--text);
}

/* ── Modal ── */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(15,23,42,.45);
    z-index: 200;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
}
.modal-overlay.open { display: flex; }

.modal {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    width: 100%;
    max-width: 520px;
    max-height: 90vh;
    overflow-y: auto;
    margin: 1rem;
    animation: modalIn .18s ease;
}
@keyframes modalIn {
    from { opacity:0; transform: translateY(-12px); }
    to   { opacity:1; transform: translateY(0); }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 1rem; font-weight: 700; }
.modal-close {
    background: none;
    border: none;
    font-size: 1.1rem;
    color: var(--text-muted);
    line-height: 1;
    padding: .2rem .4rem;
    border-radius: 4px;
    cursor: pointer;
}
.modal-close:hover { background: var(--bg); }

.modal-body   { padding: 1.1rem 1.25rem; }
.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: .6rem;
    padding: .9rem 1.25rem;
    border-top: 1px solid var(--border);
}

/* ── Toast ── */
.toast-container {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: .5rem;
    z-index: 500;
}
.toast {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: .7rem 1rem;
    border-radius: var(--radius-sm);
    font-size: .88rem;
    font-weight: 500;
    box-shadow: var(--shadow-md);
    animation: toastIn .2s ease;
    max-width: 320px;
    color: white;
}
.toast.success { background: var(--green); }
.toast.error   { background: var(--red); }
.toast.info    { background: var(--primary); }
.toast.warning { background: var(--orange); }

@keyframes toastIn {
    from { opacity:0; transform: translateX(20px); }
    to   { opacity:1; transform: translateX(0); }
}

/* ── Empty state ── */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-muted);
    font-size: .9rem;
}

/* ── Actions cell ── */
.actions-cell { display: flex; gap: .25rem; }

/* ── Responsive ── */
@media (max-width: 900px) {
    .two-col { grid-template-columns: 1fr; }
    .side-col { display: contents; }
    .stats-bar { flex-wrap: wrap; }
    .stat-card { flex: 1 1 calc(33% - .5rem); }
}
@media (max-width: 600px) {
    .nav-tab span:first-child { display: none; }
    .logo-sub { display: none; }
    .stat-card { flex: 1 1 calc(50% - .5rem); }
    .status-buttons { grid-template-columns: 1fr; }
}

/* ============================================================
   v2 Additions — RepairDesk, Templates, History Search
   ============================================================ */

/* ── Label optional hint ── */
.label-optional {
    font-weight: 400;
    color: var(--text-muted);
    font-size: .75rem;
    margin-left: .3rem;
}

/* ── Form divider ── */
.form-divider {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .2rem 1.25rem .6rem;
    font-size: .75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-muted);
}
.form-divider::before,
.form-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}

/* ── RepairDesk lookup group ── */
.rd-lookup-group {
    padding: 1.1rem 1.25rem .9rem;
    background: linear-gradient(135deg, #eff6ff 0%, #f8fafc 100%);
    border-bottom: 1px solid var(--border);
    margin-bottom: 0 !important;
}
.rd-lookup-group label { color: var(--primary); font-size: .82rem; }

.ticket-lookup-row {
    display: flex;
    gap: .5rem;
}
.ticket-lookup-row input {
    flex: 1;
    max-width: 200px;
}

.btn-rd {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    padding: .5rem 1rem;
    border-radius: var(--radius-sm);
    font-size: .88rem;
    font-weight: 600;
    border: 1.5px solid var(--primary);
    background: var(--primary);
    color: white;
    transition: all .15s;
    cursor: pointer;
    white-space: nowrap;
}
.btn-rd:hover:not(:disabled) { background: var(--primary-dark); }
.btn-rd:disabled { opacity: .6; cursor: not-allowed; }

/* ── Ticket result banner ── */
.ticket-result {
    margin-top: .6rem;
    padding: .65rem .9rem;
    border-radius: var(--radius-sm);
    font-size: .84rem;
    line-height: 1.5;
    border-left: 3px solid;
}
.ticket-result.ok  { background: var(--green-bg); border-color: var(--green); color: #065f46; }
.ticket-result.err { background: var(--red-bg);   border-color: var(--red);   color: #991b1b; }

/* ── Skeleton buttons ── */
.skeleton-btn {
    height: 44px;
    border-radius: var(--radius-sm);
    background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
    background-size: 200% 100%;
    animation: shimmer 1.4s infinite;
}
@keyframes shimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ── Status buttons: 2-column grid (more items if custom templates) ── */
.status-buttons {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: .5rem;
}

/* ── Settings card body ── */
.settings-body { padding: 0 1.25rem 1.25rem; }
.settings-body .form-group:first-child { padding-top: .5rem; }

/* ── Toolbar hint ── */
.toolbar-hint {
    font-size: .8rem;
    color: var(--text-muted);
    white-space: nowrap;
}

/* ── Templates list ── */
.templates-list { list-style: none; }
.template-item {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    transition: background .1s;
}
.template-item:last-child { border-bottom: none; }
.template-item:hover { background: #fafbfd; }

.template-icon-badge {
    width: 44px; height: 44px;
    border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    font-size: 1.3rem;
    flex-shrink: 0;
}

.template-body { flex: 1; min-width: 0; }
.template-header {
    display: flex;
    align-items: center;
    gap: .5rem;
    margin-bottom: .3rem;
    flex-wrap: wrap;
}
.template-name  { font-weight: 600; font-size: .92rem; }
.template-badge {
    font-size: .72rem;
    font-weight: 600;
    padding: .1rem .5rem;
    border-radius: 10px;
    background: var(--primary-bg);
    color: var(--primary);
}
.template-msg {
    font-size: .83rem;
    color: var(--text-muted);
    line-height: 1.45;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 560px;
}

.template-actions { display: flex; gap: .25rem; flex-shrink: 0; padding-top: .1rem; }

/* ── History stats banner ── */
.history-stats {
    padding: .6rem 1.25rem;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    font-size: .82rem;
    color: var(--text-muted);
    display: flex;
    gap: 1.5rem;
    flex-wrap: wrap;
}
.history-stats strong { color: var(--text); }

/* ── Ticket ID badge in history/repairs ── */
.ticket-badge {
    display: inline-flex;
    align-items: center;
    gap: .25rem;
    font-size: .75rem;
    font-weight: 600;
    background: #ede9fe;
    color: #5b21b6;
    padding: .15rem .5rem;
    border-radius: 4px;
    white-space: nowrap;
}

/* ── Row 3 grid for template modal ── */
.row-3 {
    display: grid;
    grid-template-columns: 80px 80px 1fr;
    gap: .75rem;
}
.row-3 .form-group { margin-bottom: 0; }

/* ── History item: ticket tag ── */
.history-ticket {
    font-size: .76rem;
    color: #5b21b6;
    background: #ede9fe;
    padding: .1rem .4rem;
    border-radius: 4px;
    font-weight: 600;
}

/* ── Repair table: ticket column ── */
.ticket-cell {
    font-size: .8rem;
    color: #5b21b6;
    font-weight: 600;
}
.ticket-cell:empty::after { content: '—'; color: var(--text-muted); font-weight: 400; }
