/* Tenant Dashboard — clean, professional MVP styling */

:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1a202c;
  --muted: #64748b;
  --primary: #1a365d;
  --primary-hover: #2c5282;
  --accent: #ed8936;
  --success-bg: #ecfdf5;
  --success-text: #065f46;
  --error-bg: #fef2f2;
  --error-text: #991b1b;
  --sidebar-width: 220px;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 0.75rem; }
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
}
.brand-mark.lg { width: 48px; height: 48px; font-size: 1rem; }
.brand-title { font-weight: 700; font-size: 0.95rem; }
.brand-sub { font-size: 0.8rem; color: var(--muted); }

.shell {
  display: flex;
  min-height: calc(100vh - 57px);
}

.sidebar {
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem 0.75rem;
  flex-shrink: 0;
  position: sticky;
  top: 57px;
  align-self: flex-start;
  height: calc(100vh - 57px);
  overflow-y: auto;
}

.nav-link {
  display: block;
  padding: 0.55rem 0.85rem;
  border-radius: 8px;
  color: var(--text);
  margin-bottom: 0.25rem;
  font-size: 0.92rem;
}
.nav-link:hover { background: #f1f5f9; text-decoration: none; }
.nav-link.active {
  background: #ebf4ff;
  color: var(--primary);
  font-weight: 600;
}

.content {
  flex: 1;
  padding: 1.75rem 2rem 3rem;
  max-width: 1280px;
}

h1 { margin: 0 0 0.35rem; font-size: 1.6rem; }
h2 { margin: 0 0 0.75rem; font-size: 1.05rem; }
.muted { color: var(--muted); margin-top: 0; }
.muted.small { font-size: 0.85rem; margin-bottom: 0.5rem; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  margin: 1.5rem 0 2rem;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
  box-shadow: var(--shadow);
}
.stat-value { font-size: 2rem; font-weight: 700; color: var(--primary); line-height: 1.1; }
.stat-label { font-size: 0.85rem; color: var(--muted); margin-top: 0.25rem; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}
.panel-header h2 { margin: 0; }

.panel-flush { padding: 0; overflow: hidden; }
.panel-flush .table-wrap { margin: 0; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
.leads-table th,
.leads-table td {
  text-align: left;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.leads-table-dense th,
.leads-table-dense td {
  padding: 0.5rem 0.65rem;
  font-size: 0.82rem;
}
.leads-table tbody tr:hover { background: #f8fafc; }
.leads-table thead th {
  background: #f8fafc;
  position: sticky;
  top: 0;
  z-index: 2;
}
.leads-table-actions .col-actions {
  position: sticky;
  right: 0;
  z-index: 1;
  background: var(--surface);
  box-shadow: -6px 0 10px -6px rgba(15, 23, 42, 0.12);
  text-align: right;
  min-width: 64px;
  padding-right: 0.75rem;
}
.leads-table-actions thead .col-actions {
  z-index: 3;
  background: #f8fafc;
}
.leads-table-actions tbody tr:hover .col-actions {
  background: #f8fafc;
}
.leads-table-wrap-dense {
  max-width: 100%;
}
.leads-table-dense .col-time { min-width: 100px; max-width: 110px; }
.leads-table-dense .col-name { min-width: 100px; max-width: 140px; }
.leads-table-dense .col-phone { min-width: 100px; max-width: 120px; }
.leads-table-dense .col-service { width: 52px; text-align: center; }
.leads-table-dense .col-issue { max-width: 280px; min-width: 120px; }
.leads-table-dense .col-quality { width: 72px; }
.name-cell {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.btn-edit {
  min-width: 52px;
}
th, td {
  text-align: left;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border);
}
th {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
}
tr:last-child td { border-bottom: none; }
.empty { text-align: center; color: var(--muted); padding: 2rem !important; }

.col-time { white-space: nowrap; min-width: 120px; font-variant-numeric: tabular-nums; }
.col-phone { white-space: nowrap; font-variant-numeric: tabular-nums; }
.col-service { white-space: nowrap; }
.col-quality { white-space: nowrap; }
.col-issue { max-width: 220px; }
.issue-text {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.col-context { min-width: 200px; max-width: 320px; }

.context-notes { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.context-chip {
  display: inline-block;
  font-size: 0.78rem;
  line-height: 1.35;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  background: #f1f5f9;
  color: #475569;
}
.context-chip strong { color: #334155; font-weight: 600; }
.context-chip-plain { background: #fff7ed; color: #9a3412; }
.context-empty { color: var(--muted); }

.empty-state {
  text-align: center;
  padding: 3rem 1.5rem;
  color: var(--muted);
}
.empty-state .empty-icon { font-size: 2rem; margin-bottom: 0.5rem; opacity: 0.7; }
.empty-state h3 { margin: 0 0 0.35rem; color: var(--text); font-size: 1.05rem; }
.empty-state p { margin: 0; max-width: 360px; margin-inline: auto; font-size: 0.9rem; }

.truncate {
  max-width: 260px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.badge-high { background: #dcfce7; color: #166534; }
.badge-medium { background: #dbeafe; color: #1e40af; }
.badge-low { background: #fee2e2; color: #991b1b; }
.badge-marginal { background: #fef3c7; color: #92400e; }
.badge-unknown { background: #f1f5f9; color: var(--muted); }
.badge-manual { background: #ede9fe; color: #5b21b6; text-transform: none; letter-spacing: 0; font-size: 0.62rem; padding: 0.1rem 0.35rem; vertical-align: middle; flex-shrink: 0; }
.badge- { background: #f1f5f9; color: var(--muted); }

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}
.toolbar .muted { margin: 0; }

.btn-sm {
  padding: 0.3rem 0.65rem;
  font-size: 0.78rem;
}

.status-ok { color: var(--success-text); }
.status-warn { color: #b45309; }

.help-details {
  margin-top: 0.75rem;
  font-size: 0.85rem;
}
.help-details summary {
  cursor: pointer;
  color: var(--primary);
  font-weight: 600;
}
.help-details .code-block { margin-top: 0.5rem; font-size: 0.78rem; }

.col-actions { width: 72px; text-align: right; white-space: nowrap; }

.form-actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-top: 0.5rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.55rem 1rem;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  background: transparent;
}
.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost {
  border-color: var(--border);
  color: var(--text);
  background: var(--surface);
}
.btn-ghost:hover { background: #f8fafc; }
.btn-block { width: 100%; }
.logout-form { margin: 0; }

.flash {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.9rem;
}
.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); }

.toast {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 100;
  padding: 0.85rem 1.15rem;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.15);
  transform: translateY(-12px);
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
}
.toast-visible { opacity: 1; transform: translateY(0); }
.toast-success {
  background: #065f46;
  color: #ecfdf5;
  border: 1px solid #047857;
}
.toast-error {
  background: #991b1b;
  color: #fef2f2;
  border: 1px solid #b91c1c;
}
.toast-warn {
  background: #92400e;
  color: #fffbeb;
  border: 1px solid #b45309;
}

.form-actions-sticky {
  position: sticky;
  bottom: 0;
  background: linear-gradient(to top, var(--surface) 80%, transparent);
  padding: 1rem 0 0.25rem;
  margin-top: 1rem;
}

/* Service catalog editor */
.catalog-editor-section { margin-bottom: 0; }
.catalog-editor {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin: 1rem 0;
}
.catalog-category {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  background: #fafbfc;
}
.catalog-category-header {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}
.catalog-category-name { flex: 1; min-width: 200px; margin: 0; }
.catalog-children {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}
.catalog-child {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.85rem;
  position: relative;
}
.catalog-child-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem 0.85rem;
  padding-right: 4rem;
}
.catalog-child-grid .span-2 { grid-column: 1 / -1; }
.catalog-child .btn-remove-child {
  position: absolute;
  top: 0.65rem;
  right: 0.65rem;
}
.hint {
  font-weight: 400;
  color: var(--muted);
  font-size: 0.78rem;
}

.form-panel section {
  margin-bottom: 1.75rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--border);
}
.form-panel section:last-of-type { border-bottom: none; }

.field-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
}
.field-grid.colors { grid-template-columns: repeat(3, minmax(120px, 1fr)); }

label span {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  margin-bottom: 0.35rem;
  color: #334155;
}

input[type="text"],
input[type="url"],
input[type="password"],
input[type="email"],
select,
textarea {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.92rem;
  font-family: inherit;
  background: #fff;
}
input[type="color"] {
  width: 100%;
  height: 42px;
  padding: 0.2rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
textarea.code-input {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
  line-height: 1.45;
}

.checkbox-group { display: flex; flex-wrap: wrap; gap: 1rem; }
.checkbox {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.92rem;
  cursor: pointer;
}
.checkbox input { width: auto; }

.code-block {
  background: #0f172a;
  color: #e2e8f0;
  padding: 1rem 1.25rem;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 0.85rem;
  line-height: 1.5;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-all;
}

.help-list {
  margin: 0;
  padding-left: 1.25rem;
  color: var(--muted);
  font-size: 0.9rem;
}
.help-list code {
  background: #f1f5f9;
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-size: 0.82rem;
}

.link { font-size: 0.88rem; font-weight: 600; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* Login page */
.login-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 2rem;
  box-shadow: 0 8px 30px rgba(15, 23, 42, 0.08);
}
.login-header { text-align: center; margin-bottom: 1.5rem; }
.login-header h1 { font-size: 1.35rem; margin: 0.75rem 0 0.25rem; }
.login-header p { margin: 0; color: var(--muted); font-size: 0.9rem; }
.login-form label { display: block; margin-bottom: 1rem; }
.login-form .btn { margin-top: 0.5rem; }

@media (max-width: 768px) {
  .shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--border);
    display: flex;
    gap: 0.25rem;
    overflow-x: auto;
    padding: 0.5rem;
    position: sticky;
    top: 57px;
    height: auto;
    z-index: 9;
  }
  .nav-link { white-space: nowrap; margin: 0; }
  .content { padding: 1.25rem; }

  .catalog-child-grid {
    grid-template-columns: 1fr;
    padding-right: 0;
    padding-top: 2rem;
  }
  .catalog-child .btn-remove-child {
    top: 0.5rem;
    right: 0.5rem;
  }
  .catalog-category-header { flex-direction: column; align-items: stretch; }

  .leads-table thead { display: none; }
  .leads-table-actions .col-actions {
    position: static;
    box-shadow: none;
    order: -1;
    justify-content: flex-end;
    padding-bottom: 0.5rem;
    margin-bottom: 0.25rem;
    border-bottom: 1px dashed var(--border);
  }
  .leads-table tbody tr {
    display: flex;
    flex-direction: column;
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--border);
    position: relative;
  }
  .leads-table tbody tr:hover { background: transparent; }
  .leads-table tbody td {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.3rem 0;
    border: none;
    text-align: right;
  }
  .leads-table tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
    text-align: left;
    flex-shrink: 0;
  }
  .leads-table .col-actions::before { content: none; }
  .leads-table .col-actions {
    display: flex;
    justify-content: flex-end;
  }
  .leads-table .col-issue {
    flex-direction: column;
    align-items: flex-start;
    text-align: left;
  }
  .leads-table .col-issue::before { margin-bottom: 0.25rem; }
  .name-cell { max-width: none; white-space: normal; }
  .issue-text { white-space: normal; }
  .toast { left: 1rem; right: 1rem; }
  .form-actions-sticky { position: static; }
}
