:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --border: #e3e8f0;
  --text: #1f2733;
  --muted: #6b7686;
  --primary: #3b6ef5;
  --primary-dark: #2f59cf;
  --danger: #e5484d;
  --warn: #e8a13a;
  --success: #2f9e6b;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.06), 0 1px 2px rgba(16, 24, 40, 0.04);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 15px;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.05rem; }
p { margin: 0.4rem 0; }
code { background: #eef1f7; padding: 1px 5px; border-radius: 4px; }

/* Topbar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { max-width: 1200px; margin: 0 auto; padding: 0.6rem 1rem; display: flex; align-items: center; gap: 1rem; }
.brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 600; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand-mark { background: var(--primary); color: #fff; border-radius: 6px; padding: 0.15rem 0.45rem; font-size: 0.8rem; font-weight: 700; }
.brand-name { font-size: 1rem; }

.nav { display: flex; align-items: center; gap: 0.25rem; flex: 1; }
.nav a { color: var(--text); padding: 0.4rem 0.7rem; border-radius: 8px; font-weight: 500; }
.nav a:hover { background: #f0f3fa; text-decoration: none; }
.nav a.active { background: #e8eefc; color: var(--primary-dark); }
.nav a.nav-new { margin-left: 0.4rem; }

.topbar-right { display: flex; align-items: center; gap: 0.7rem; margin-left: auto; }
.user-chip { display: flex; align-items: center; gap: 0.4rem; }
.user-name { font-weight: 600; font-size: 0.9rem; }
.lang-switch { font-size: 0.8rem; font-weight: 700; letter-spacing: 0.04em; color: var(--muted); border: 1px solid var(--border); padding: 0.25rem 0.5rem; border-radius: 6px; }
.lang-switch:hover { color: var(--primary); border-color: var(--primary); }
.inline-form { display: inline; }

/* Layout */
.container { max-width: 1200px; margin: 0 auto; padding: 1.25rem 1rem 3rem; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.1rem; }
.page-head h1 { margin: 0; }
.back-link { display: inline-block; font-size: 0.85rem; margin-bottom: 0.2rem; color: var(--muted); }
.detail-title { font-size: 1.35rem; display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
.ticket-no { color: var(--muted); font-weight: 500; font-size: 1rem; }
.detail-badges { display: flex; gap: 0.4rem; margin-top: 0.5rem; flex-wrap: wrap; }
.detail-grid { display: grid; grid-template-columns: 1fr 320px; gap: 1rem; align-items: start; }
.detail-main, .detail-side { display: flex; flex-direction: column; gap: 1rem; min-width: 0; }
.detail-side { position: sticky; top: 70px; }

/* Flash */
.flash { padding: 0.7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; font-weight: 500; }
.flash-error { background: #fdecec; color: #b3261e; border: 1px solid #f8c9c7; }
.flash-success { background: #e9f7ef; color: #1e7d4f; border: 1px solid #b7e4cc; }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem; font-family: inherit; font-size: 0.92rem; font-weight: 600; padding: 0.5rem 1rem; border-radius: 8px; border: 1px solid transparent; cursor: pointer; transition: background 0.12s, border-color 0.12s; }
.btn:hover { text-decoration: none; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: #f0f3fa; }
.btn-warn { background: var(--warn); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(0.93); }
.btn-sm { padding: 0.35rem 0.7rem; font-size: 0.85rem; }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); margin-bottom: 1rem; }
.card-title { font-size: 0.95rem; font-weight: 700; margin: 0 0 0.7rem; text-transform: none; letter-spacing: 0.01em; }
.table-card { padding: 0.25rem 0; }
.table-card .table { margin: 0; }
.description { white-space: pre-wrap; word-break: break-word; }

/* Stat grid */
.stat-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 0.75rem; margin-bottom: 1.1rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.85rem 1rem; box-shadow: var(--shadow); }
.stat-num { font-size: 1.7rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: 0.8rem; }
.stat-critical .stat-num { color: var(--danger); }
.stat-open .stat-num { color: var(--primary); }
.stat-resolved .stat-num { color: var(--success); }

.dash-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; align-items: start; margin-bottom: 1rem; }

/* Lists */
.list-plain, .list-breach, .activity { list-style: none; margin: 0; padding: 0; }
.list-plain li, .list-breach li, .activity li { padding: 0.55rem 0; border-bottom: 1px solid var(--border); }
.list-plain li:last-child, .list-breach li:last-child, .activity li:last-child { border-bottom: none; }
.breach-subject { font-weight: 500; margin-left: 0.4rem; }
.breach-detail { display: block; font-size: 0.8rem; color: var(--muted); margin-left: 0; padding-left: 0; }
.list-breach li { display: block; }
.activity-action { font-weight: 500; margin: 0 0.3rem; }
.activity-time { font-size: 0.78rem; margin-left: 0.4rem; }

/* Table */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 0.6rem 0.8rem; border-bottom: 1px solid var(--border); }
.table th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; }
.table tbody tr:hover { background: #f8faff; }
.table td { font-size: 0.9rem; }
.td-subject { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { padding: 1.5rem; text-align: center; }

/* Badges */
.badge { display: inline-flex; align-items: center; padding: 0.15rem 0.55rem; border-radius: 999px; font-size: 0.75rem; font-weight: 600; white-space: nowrap; }
.status-open { background: #e8eefc; color: #2f59cf; }
.status-in_progress { background: #eef1f7; color: #4a5568; }
.status-on_hold { background: #fff4e3; color: #b47712; }
.status-resolved { background: #e9f7ef; color: #1e7d4f; }
.status-closed { background: #eef1f7; color: #6b7686; }
.priority-low { background: #eef1f7; color: #4a5568; }
.priority-normal { background: #e8eefc; color: #2f59cf; }
.priority-high { background: #fff4e3; color: #b47712; }
.priority-critical { background: #fdecec; color: #b3261e; }
.sla-met { background: #e9f7ef; color: #1e7d4f; }
.sla-breach { background: #fdecec; color: #b3261e; }
.sla-pending { background: #eef1f7; color: #4a5568; }
.sla-paused { background: #fff4e3; color: #b47712; }
.badge-internal { background: #f3e8ff; color: #7c3aed; }
.badge-role { background: #eef1f7; color: #4a5568; }
.badge-tag { background: #eef1f7; color: #4a5568; font-size: 0.7rem; }

/* Forms */
.field { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.75rem; }
.field label { font-size: 0.85rem; font-weight: 600; }
.field input, .field select, .field textarea, .filter-bar input, .filter-bar select, .reply-form textarea {
  font-family: inherit; font-size: 0.92rem; padding: 0.5rem 0.65rem; border: 1px solid var(--border); border-radius: 8px; background: #fff; color: var(--text); width: 100%;
}
.field input:focus, .field select:focus, .field textarea:focus, .filter-bar input:focus, .filter-bar select:focus, .reply-form textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.field-error { color: var(--danger); font-size: 0.8rem; }
.form-card { max-width: 720px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
.form-grid .span-2 { grid-column: span 2; }
.form-actions { display: flex; gap: 0.6rem; margin-top: 0.5rem; }
.stack-form .field { margin-bottom: 0.6rem; }
.stack-form .btn { width: 100%; margin-top: 0.3rem; }

.filter-bar { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.7rem; box-shadow: var(--shadow); }
.filter-bar input, .filter-bar select { width: auto; min-width: 140px; }
.filter-bar input[type="search"] { flex: 1; min-width: 180px; }
.filter-bar .btn { width: auto; }

/* Thread / messages */
.thread { display: flex; flex-direction: column; gap: 0.75rem; margin-bottom: 1rem; }
.message { border: 1px solid var(--border); border-radius: var(--radius); padding: 0.7rem 0.9rem; }
.message-internal { background: #faf7ff; border-color: #e6d9fb; }
.message-head { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.3rem; }
.message-sender { font-weight: 600; font-size: 0.9rem; }
.message-time { font-size: 0.78rem; margin-left: auto; }
.message-body { white-space: pre-wrap; word-break: break-word; }

.reply-box { border-top: 1px solid var(--border); padding-top: 0.9rem; margin-top: 0.4rem; }
.reply-tabs { display: flex; gap: 0.4rem; margin-bottom: 0.6rem; }
.reply-tab { font-family: inherit; font-size: 0.85rem; font-weight: 600; padding: 0.35rem 0.75rem; border: 1px solid var(--border); background: #fff; border-radius: 8px; cursor: pointer; }
.reply-tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.reply-actions { margin-top: 0.5rem; }

/* Meta list */
.meta-list { margin: 0; }
.meta-list > div { display: flex; justify-content: space-between; gap: 0.75rem; padding: 0.4rem 0; border-bottom: 1px solid var(--border); }
.meta-list > div:last-child { border-bottom: none; }
.meta-list dt { color: var(--muted); font-size: 0.85rem; }
.meta-list dd { margin: 0; font-weight: 500; text-align: right; }

/* Add asset */
.add-asset-form { margin-top: 0.9rem; border-top: 1px dashed var(--border); padding-top: 0.8rem; }
.add-asset-row { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.add-asset-row input, .add-asset-row select { font-family: inherit; font-size: 0.88rem; padding: 0.4rem 0.55rem; border: 1px solid var(--border); border-radius: 8px; }
.add-asset-row input[type="text"] { flex: 1; min-width: 120px; }

/* Login */
.login-wrap { display: flex; justify-content: center; padding: 3rem 1rem; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 2rem; width: 100%; max-width: 400px; }
.login-title { font-size: 1.4rem; margin: 0 0 0.3rem; }
.login-sub { color: var(--muted); margin-bottom: 1.3rem; font-size: 0.9rem; }
.portal-tabs { display: flex; gap: 0.4rem; margin-bottom: 1.2rem; background: #f0f3fa; padding: 0.3rem; border-radius: 10px; }
.portal-tab { flex: 1; font-family: inherit; font-size: 0.88rem; font-weight: 600; padding: 0.5rem; border: none; background: transparent; border-radius: 8px; cursor: pointer; color: var(--muted); }
.portal-tab.active { background: #fff; color: var(--text); box-shadow: var(--shadow); }
.login-form .btn { margin-top: 0.4rem; }

/* Reports page */
.report-cols { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; align-items: start; margin-bottom: 1rem; }

/* Archive card */
.archive-card { border-left: 3px solid var(--danger); }

/* Footer */
.footer { border-top: 1px solid var(--border); background: var(--surface); margin-top: 2rem; }
.footer-inner { display: flex; justify-content: space-between; padding: 1rem; color: var(--muted); font-size: 0.85rem; }

.muted { color: var(--muted); }

/* Print (reports) */
@media print {
  .topbar, .footer, .no-print, .flash { display: none !important; }
  body { background: #fff; }
  .stat, .card { box-shadow: none; }
  .report-cols { grid-template-columns: repeat(3, 1fr); }
  /* Keep tables as full-width tables in print: scroll containers clip
     overflow in print, so undo the mobile display:block/nowrap there. */
  .table { display: table; overflow: visible; }
  .table th, .table td { white-space: normal; }
}

/* Responsive */
@media (max-width: 900px) {
  .detail-grid { grid-template-columns: 1fr; }
  .detail-side { position: static; }
  .report-cols { grid-template-columns: 1fr; }
  .stat-grid { grid-template-columns: repeat(3, 1fr); }
  .dash-cols { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: span 1; }
  .topbar-inner { flex-wrap: wrap; }
  .nav { order: 3; width: 100%; overflow-x: auto; }
}

/* Tablets and large phones */
@media (max-width: 700px) {
  /* Data tables scroll inside their card instead of forcing page-level overflow */
  .table { display: block; overflow-x: auto; }
  .table th, .table td { white-space: nowrap; }
  .table .td-subject { max-width: 260px; }

  /* iOS Safari auto-zooms into inputs narrower than 16px on focus */
  .field input, .field select, .field textarea,
  .filter-bar input, .filter-bar select,
  .add-asset-row input, .add-asset-row select,
  .reply-form textarea { font-size: 16px; }

  /* Bigger touch targets, stacked actions and full-width filter controls */
  .nav a { padding: 0.55rem 0.85rem; }
  .page-head { flex-direction: column; align-items: stretch; }
  .form-actions { flex-direction: column; }
  .filter-bar { flex-direction: column; }
  .page-head > .btn, .form-actions .btn, .reply-actions .btn, .filter-bar .btn { width: 100%; }
  .filter-bar input, .filter-bar select { width: 100%; min-width: 0; }
}

/* Phones */
@media (max-width: 560px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .container { padding: 0.85rem 0.75rem 2.5rem; }
  .footer-inner { padding: 0.85rem 0.75rem; }
  .card { padding: 0.8rem 0.85rem; }
  .table-card { padding: 0.25rem 0; }
  .topbar .user-chip { display: none; }
  .topbar-right { gap: 0.45rem; }
  .detail-title { font-size: 1.2rem; }
}
