/* =====================================================================
   Tapestry — application styles
   Palette: warm neutral canvas, deep teal accent, garnet highlight.
   Type: IBM Plex Sans (UI), Fraunces (wordmark / display).
   ===================================================================== */

:root {
    --ink: #1c2321;
    --ink-soft: #47524d;
    --ink-faint: #7c8a83;
    --line: #e2e0d8;
    --line-strong: #d0cdc2;
    --canvas: #f6f4ef;
    --surface: #ffffff;
    --surface-2: #fbfaf6;
    --accent: #0f6b5c;
    --accent-dark: #0a4d42;
    --accent-soft: #e3f0ec;
    --garnet: #8c2f39;
    --garnet-soft: #f6e7e6;
    --amber: #b7791f;
    --amber-soft: #f7ecd6;
    --ok: #0f6b5c;
    --warn: #b7791f;
    --danger: #b23b3b;
    --danger-soft: #f7e4e2;
    --shadow-sm: 0 1px 2px rgba(28, 35, 33, .06);
    --shadow-md: 0 6px 24px rgba(28, 35, 33, .10);
    --radius: 12px;
    --radius-sm: 8px;
    --sidebar-w: 252px;
    --font-ui: "IBM Plex Sans", ui-sans-serif, sans-serif;
    --font-display: "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
}

body {
    font-family: var(--font-ui);
    color: var(--ink);
    background: var(--canvas);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3, h4 { margin: 0 0 .4em; font-weight: 600; line-height: 1.2; }

/* ---------------------------------------------------------------- Shell */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w);
    flex: 0 0 var(--sidebar-w);
    background: linear-gradient(180deg, #10312b 0%, #0c241f 100%);
    color: #d8e5e0;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    z-index: 40;
}

.brand {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 18px 14px;
}
.brand-mark { display: flex; align-items: center; gap: 10px; color: #fff; }
.brand-mark:hover { text-decoration: none; }
.brand-glyph {
    font-size: 22px;
    color: #e7c26b;
    line-height: 1;
}
.brand-name {
    font-family: var(--font-display);
    font-size: 24px;
    font-weight: 600;
    letter-spacing: .3px;
    color: #fbfaf6;
}
.sidebar-close { display: none; background: none; border: 0; color: #cfe0da; font-size: 26px; cursor: pointer; }

.nav { flex: 1; overflow-y: auto; padding: 8px 12px; }
.nav-section {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .12em;
    color: #6f8b82;
    padding: 16px 10px 6px;
}
.nav-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    color: #c6d6d0;
    font-weight: 500;
    margin-bottom: 2px;
    transition: background .15s, color .15s;
}
.nav-link:hover { background: rgba(255,255,255,.06); color: #fff; text-decoration: none; }
.nav-link.is-active { background: rgba(231,194,107,.16); color: #fff; }
.nav-link.is-active .nav-icon { background: #e7c26b; }
.nav-link.is-disabled { opacity: .45; cursor: default; }
.nav-icon {
    width: 18px; height: 18px; flex: 0 0 18px;
    background: #7fa79c;
    mask-repeat: no-repeat; mask-position: center; mask-size: contain;
    -webkit-mask-repeat: no-repeat; -webkit-mask-position: center; -webkit-mask-size: contain;
}
.nav-badge {
    margin-left: auto; font-size: 10px; text-transform: uppercase; letter-spacing: .08em;
    background: rgba(255,255,255,.12); padding: 2px 6px; border-radius: 20px;
}
.sidebar-foot { padding: 14px; border-top: 1px solid rgba(255,255,255,.08); }
.user-chip { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.user-avatar {
    width: 34px; height: 34px; border-radius: 50%;
    background: #e7c26b; color: #10312b; font-weight: 700;
    display: grid; place-items: center; font-size: 15px;
}
.user-meta { display: flex; flex-direction: column; line-height: 1.15; }
.user-name { font-weight: 600; color: #fff; font-size: 14px; }
.user-role { font-size: 12px; color: #8fb0a7; }
.btn-logout {
    display: block; text-align: center; padding: 8px; border-radius: var(--radius-sm);
    background: rgba(255,255,255,.08); color: #dcebe6; font-weight: 500; font-size: 14px;
}
.btn-logout:hover { background: rgba(255,255,255,.16); text-decoration: none; }

.sidebar-scrim {
    display: none; position: fixed; inset: 0; background: rgba(12,20,18,.5); z-index: 35;
}

/* ---------------------------------------------------------------- Main */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
    position: sticky; top: 0; z-index: 30;
    display: flex; align-items: center; gap: 14px;
    padding: 14px 26px;
    background: rgba(246,244,239,.86);
    backdrop-filter: saturate(1.4) blur(8px);
    border-bottom: 1px solid var(--line);
}
.page-title { font-size: 20px; margin: 0; font-weight: 600; }
.topbar-actions { margin-left: auto; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.menu-toggle {
    display: none; flex-direction: column; gap: 4px; background: none; border: 0; cursor: pointer; padding: 6px;
}
.menu-toggle span { width: 22px; height: 2px; background: var(--ink); border-radius: 2px; }

.content { padding: 24px 26px 48px; flex: 1; }

/* ---------------------------------------------------------------- Buttons */
.btn {
    display: inline-flex; align-items: center; gap: 7px;
    padding: 9px 16px; border-radius: var(--radius-sm);
    border: 1px solid transparent; font: inherit; font-weight: 600; font-size: 14px;
    cursor: pointer; background: var(--surface); color: var(--ink);
    border-color: var(--line-strong); transition: background .15s, border-color .15s, box-shadow .15s;
    white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { filter: brightness(.94); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn-ghost:hover { background: var(--accent-soft); }
.btn-sm { padding: 6px 11px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-block { width: 100%; justify-content: center; }

/* ---------------------------------------------------------------- Cards / panels */
.panel {
    background: var(--surface); border: 1px solid var(--line);
    border-radius: var(--radius); box-shadow: var(--shadow-sm);
}
.panel-pad { padding: 20px; }
.panel-head {
    display: flex; align-items: center; gap: 12px;
    padding: 16px 20px; border-bottom: 1px solid var(--line);
}
.panel-head h2 { margin: 0; font-size: 16px; }
.panel-head .spacer { margin-left: auto; }

.grid { display: grid; gap: 18px; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }

/* Stat cards */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; }
.stat {
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 18px 20px; box-shadow: var(--shadow-sm);
}
.stat .stat-label { font-size: 13px; color: var(--ink-faint); font-weight: 500; }
.stat .stat-value { font-size: 30px; font-weight: 700; font-family: var(--font-display); margin-top: 4px; }
.stat .stat-sub { font-size: 12.5px; color: var(--ink-faint); margin-top: 2px; }
.stat.accent { border-left: 4px solid var(--accent); }
.stat.garnet { border-left: 4px solid var(--garnet); }
.stat.amber  { border-left: 4px solid var(--amber); }

/* ---------------------------------------------------------------- Tables */
.table-wrap { overflow-x: auto; border-radius: var(--radius); }
table.data { width: 100%; border-collapse: collapse; background: var(--surface); font-size: 14px; }
table.data th, table.data td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--line); }
table.data th {
    font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-faint);
    background: var(--surface-2); font-weight: 600; position: sticky; top: 0; white-space: nowrap;
}
table.data tbody tr { cursor: default; }
table.data tbody tr.clickable { cursor: pointer; }
table.data tbody tr.clickable:hover { background: var(--accent-soft); }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-empty { padding: 40px; text-align: center; color: var(--ink-faint); }

/* ---------------------------------------------------------------- Badges */
.badge {
    display: inline-flex; align-items: center; gap: 5px;
    padding: 3px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; white-space: nowrap;
    background: var(--surface-2); color: var(--ink-soft); border: 1px solid var(--line-strong);
}
.badge-instock { background: var(--accent-soft); color: var(--accent-dark); border-color: transparent; }
.badge-onmemo  { background: var(--amber-soft); color: #8a5a12; border-color: transparent; }
.badge-sold    { background: #e7e5df; color: #55605a; border-color: transparent; }
.badge-onhold  { background: var(--garnet-soft); color: var(--garnet); border-color: transparent; }
.badge-active  { background: var(--accent-soft); color: var(--accent-dark); border-color: transparent; }
.badge-overdue { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge-paid    { background: var(--accent-soft); color: var(--accent-dark); border-color: transparent; }
.badge-unpaid  { background: var(--amber-soft); color: #8a5a12; border-color: transparent; }
.badge-partial { background: var(--amber-soft); color: #8a5a12; border-color: transparent; }
.badge-void    { background: #e7e5df; color: #55605a; border-color: transparent; }

/* ---------------------------------------------------------------- Forms */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px 16px; }
.form-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.form-section {
    margin-bottom: 22px; padding-bottom: 18px; border-bottom: 1px solid var(--line);
}
.form-section:last-of-type { border-bottom: 0; margin-bottom: 0; padding-bottom: 0; }
.form-section h3 {
    font-family: var(--font-display); font-size: 17px; font-weight: 600;
    margin: 0 0 12px; color: var(--ink);
}
.field { display: flex; flex-direction: column; gap: 5px; }
.field.full { grid-column: 1 / -1; }
.field.compact { max-width: 140px; }
.field.compact-sm { max-width: 100px; }
.field label { font-size: 13px; font-weight: 600; color: var(--ink-soft); }
.field .hint { font-size: 12px; color: var(--ink-faint); }

/* Unify text fields + selects everywhere (including inputs with no type attr). */
input:not([type]),
input[type=text], input[type=email], input[type=password], input[type=number],
input[type=date], input[type=search], input[type=tel], input[type=url],
input[type=file], input[type=time], input[type=datetime-local],
select, textarea {
    width: 100%; height: 40px; padding: 8px 11px; border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm); font: inherit; font-size: 14px; line-height: 1.25; color: var(--ink);
    background: var(--surface); transition: border-color .15s, box-shadow .15s;
    box-sizing: border-box; margin: 0;
    -webkit-appearance: none; appearance: none;
}
select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%2347524d' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 12px center; padding-right: 32px;
}
textarea { height: auto; min-height: 84px; resize: vertical; padding-top: 10px; line-height: 1.4; }
input:focus, select:focus, textarea:focus {
    outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
input[type=checkbox], input[type=radio] {
    width: auto; height: auto; padding: 0; border: initial; border-radius: 0;
    background: initial; box-shadow: none; -webkit-appearance: auto; appearance: auto;
}
.checkbox { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.checkbox input { width: auto; height: auto; }

/* Toolbar filters match field controls */
.toolbar select,
.toolbar input:not([type]),
.toolbar input[type=text],
.toolbar input[type=search],
.toolbar input[type=date] {
    height: 40px; min-width: 140px;
}

@media (max-width: 720px) {
    .form-grid, .form-grid.cols-4 { grid-template-columns: 1fr 1fr; }
    .field.compact, .field.compact-sm { max-width: none; }
}
@media (max-width: 480px) {
    .form-grid, .form-grid.cols-4 { grid-template-columns: 1fr; }
}

/* Role hover flyout */
.role-tip { position: relative; cursor: help; text-decoration: underline dotted; }
.role-flyout {
    display: none; position: absolute; left: 0; bottom: calc(100% + 8px);
    width: 280px; padding: 12px 14px; background: #10312b; color: #f3f2ee;
    border-radius: 10px; font-size: 12px; font-weight: 400; line-height: 1.45;
    box-shadow: 0 10px 30px rgba(0,0,0,.25); z-index: 40;
}
.role-flyout strong { color: #e7c26b; }
.role-tip:hover .role-flyout, .role-tip:focus .role-flyout, .role-tip:focus-within .role-flyout { display: block; }

/* Rug history timeline */
.timeline { list-style: none; margin: 0; padding: 0 0 0 8px; border-left: 2px solid var(--line-strong); }
.timeline-item { position: relative; padding: 0 0 18px 18px; }
.timeline-item:last-child { padding-bottom: 0; }
.timeline-dot {
    position: absolute; left: -7px; top: 4px; width: 12px; height: 12px;
    border-radius: 50%; background: var(--accent); border: 2px solid var(--surface);
}
.timeline-title { font-weight: 600; font-size: 14px; }
.timeline-meta { font-size: 12px; color: var(--ink-faint); margin-top: 2px; }
.timeline-notes { font-size: 13px; color: var(--ink-soft); margin-top: 4px; white-space: pre-wrap; }

.import-preview { max-height: 360px; overflow: auto; }
.import-err { color: var(--danger); font-size: 12px; }

/* Filter / toolbar row */
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 18px; }
.toolbar .search { flex: 1; min-width: 220px; position: relative; }
.toolbar .search input { padding-left: 36px; }
.toolbar .search::before {
    content: ""; position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
    width: 15px; height: 15px; opacity: .5;
    background: var(--ink-faint);
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2'%3E%3Ccircle cx='11' cy='11' r='8'/%3E%3Cpath d='m21 21-4.3-4.3'/%3E%3C/svg%3E") center/contain no-repeat;
    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2'%3E%3Ccircle cx='11' cy='11' r='8'/%3E%3Cpath d='m21 21-4.3-4.3'/%3E%3C/svg%3E") center/contain no-repeat;
}

/* Detail layout */
.detail-head { display: flex; align-items: flex-start; gap: 18px; margin-bottom: 22px; flex-wrap: wrap; }
.detail-head .title-block { flex: 1; min-width: 240px; }
.detail-head h1 { font-size: 26px; font-family: var(--font-display); margin-bottom: 4px; }
.detail-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: 150px 1fr; gap: 8px 14px; font-size: 14px; }
.kv dt { color: var(--ink-faint); font-weight: 500; }
.kv dd { margin: 0; }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; flex-wrap: wrap; }
.tab {
    padding: 10px 14px; border: 0; background: none; font: inherit; font-weight: 600; font-size: 14px;
    color: var(--ink-faint); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); }
.tab-panel { display: none; }
.tab-panel.is-active { display: block; }

/* Photo / media */
.photo-frame {
    width: 100%; aspect-ratio: 4/3; border-radius: var(--radius); overflow: hidden;
    background: var(--surface-2) center/cover no-repeat; border: 1px solid var(--line);
    display: grid; place-items: center; color: var(--ink-faint);
}
.photo-frame img { width: 100%; height: 100%; object-fit: cover; }
.thumb-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.thumb { width: 62px; height: 62px; border-radius: 8px; object-fit: cover; border: 2px solid var(--line); cursor: pointer; }
.thumb.is-primary { border-color: var(--accent); }

/* Line item editor */
.line-table { width: 100%; border-collapse: collapse; }
.line-table th { font-size: 12px; text-transform: uppercase; color: var(--ink-faint); text-align: left; padding: 6px 8px; }
.line-table td { padding: 5px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.line-table input, .line-table select {
    height: 36px; padding: 6px 10px; font-size: 13px;
}
.line-table select { padding-right: 28px; }

/* Toast */
.toast-stack { position: fixed; bottom: 22px; right: 22px; display: flex; flex-direction: column; gap: 10px; z-index: 100; }
.toast {
    background: var(--ink); color: #fff; padding: 12px 16px; border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md); font-size: 14px; max-width: 340px;
    animation: toast-in .2s ease; display: flex; gap: 10px; align-items: center;
}
.toast.ok { background: var(--accent-dark); }
.toast.error { background: var(--danger); }
@keyframes toast-in { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Modal */
.modal-root { position: fixed; inset: 0; z-index: 90; display: grid; place-items: center; padding: 20px; }
.modal-root[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(20,28,26,.5); animation: fade .15s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal-panel {
    position: relative; background: var(--surface); border-radius: var(--radius);
    box-shadow: var(--shadow-md); width: min(720px, 100%); max-height: 90vh; display: flex; flex-direction: column;
    animation: modal-in .18s ease;
}
@keyframes modal-in { from { transform: translateY(12px) scale(.98); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-title { font-size: 17px; margin: 0; }
.modal-close { margin-left: auto; background: none; border: 0; font-size: 24px; cursor: pointer; color: var(--ink-faint); }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: flex-end; }

/* Utility */
.muted { color: var(--ink-faint); }
.mono { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.mt-0 { margin-top: 0; } .mt-1 { margin-top: 8px; } .mt-2 { margin-top: 16px; } .mt-3 { margin-top: 24px; }
.mb-2 { margin-bottom: 16px; }
.flex { display: flex; } .flex-between { display: flex; align-items: center; justify-content: space-between; }
.gap-2 { gap: 12px; } .wrap { flex-wrap: wrap; }
.pill-row { display: flex; gap: 8px; flex-wrap: wrap; }
.loading { padding: 40px; text-align: center; color: var(--ink-faint); }
.spinner { display:inline-block; width:18px; height:18px; border:2px solid var(--line-strong); border-top-color: var(--accent); border-radius:50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.hidden { display: none !important; }
.empty-hint { text-align: center; color: var(--ink-faint); padding: 30px; }

/* Pagination */
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 16px; font-size: 14px; }

/* Login page */
.auth-wrap {
    min-height: 100vh; display: grid; grid-template-columns: 1.1fr 1fr;
    background: var(--canvas);
}
.auth-visual {
    background:
        radial-gradient(120% 120% at 10% 10%, rgba(231,194,107,.22), transparent 55%),
        linear-gradient(155deg, #10312b, #0a221d 70%);
    color: #f6f4ef; padding: 56px; display: flex; flex-direction: column; justify-content: space-between;
    position: relative; overflow: hidden;
}
.auth-visual::after {
    content: ""; position: absolute; inset: 0; opacity: .10;
    background-image: repeating-linear-gradient(45deg, #e7c26b 0 2px, transparent 2px 22px),
                      repeating-linear-gradient(-45deg, #e7c26b 0 2px, transparent 2px 22px);
    mix-blend-mode: overlay;
}
.auth-visual .brand-name { font-size: 40px; }
.auth-tagline { font-family: var(--font-display); font-size: 30px; line-height: 1.25; max-width: 12ch; position: relative; }
.auth-visual p.sub { color: #b7ccc4; max-width: 40ch; position: relative; }
.auth-form-col { display: grid; place-items: center; padding: 40px; }
.auth-card { width: min(400px, 100%); }
.auth-card h1 { font-family: var(--font-display); font-size: 28px; }
.auth-error { background: var(--danger-soft); color: var(--danger); padding: 10px 14px; border-radius: var(--radius-sm); font-size: 14px; margin-bottom: 14px; }

/* Barcode scan input */
.scan-box { display: flex; gap: 8px; align-items: center; }
.scan-box input { font-size: 16px; letter-spacing: .04em; }

/* Print label sheet */
@media print {
    .sidebar, .topbar, .no-print { display: none !important; }
    .main, .content { padding: 0 !important; }
    body { background: #fff; }
}

/* ---------------------------------------------------------------- Responsive */
@media (max-width: 980px) {
    .form-grid, .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
    .auth-wrap { grid-template-columns: 1fr; }
    .auth-visual { display: none; }
}

@media (max-width: 760px) {
    .sidebar {
        position: fixed; left: 0; top: 0; transform: translateX(-100%);
        transition: transform .22s ease; box-shadow: var(--shadow-md);
    }
    body.nav-open .sidebar { transform: none; }
    body.nav-open .sidebar-scrim { display: block; }
    .sidebar-close { display: block; }
    .menu-toggle { display: flex; }
    .content { padding: 18px 16px 40px; }
    .topbar { padding: 12px 16px; }
    .page-title { font-size: 18px; }
    .kv { grid-template-columns: 1fr; gap: 2px 0; }
    .kv dt { margin-top: 8px; }
    .detail-actions { width: 100%; }
}
