:root {
  --bg: #e8eef5;
  --surface: #ffffff;
  --ink: #122033;
  --muted: #5a6f88;
  --brand: #1a5f8a;
  --brand-2: #2a8fbf;
  --accent: #1e7aab;
  --ok: #1f7a4c;
  --warn: #b8860b;
  --danger: #b42318;
  --line: #c5d4e3;
  --radius: 12px;
  --shadow: 0 10px 30px rgba(18, 40, 70, 0.12);
  --font: "Segoe UI", "PT Sans", "Trebuchet MS", sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; min-height: 100%;
  font-family: var(--font); color: var(--ink); background: var(--bg);
}
button, input, select, textarea { font: inherit; }
.hidden { display: none !important; }

.login-screen {
  min-height: 100vh; display: grid; place-items: center;
  background: linear-gradient(165deg, #d8e8f4 0%, #eef4fa 48%, #cfe0ef 100%);
  padding: 20px;
}
.login-card {
  width: min(400px, 100%); background: var(--surface); border-radius: 18px;
  padding: 28px; box-shadow: var(--shadow); display: grid; gap: 12px;
}
.brand { font-size: 1.8rem; font-weight: 700; color: var(--brand); letter-spacing: -0.02em; }
.login-sub { margin: -4px 0 8px; color: var(--muted); }
.login-card label { display: grid; gap: 6px; font-size: 0.9rem; color: var(--muted); }
.login-card input, .content input, .content select, .content textarea, .modal-panel input,
.modal-panel select, .modal-panel textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.login-card button, .btn {
  border: 0; border-radius: 10px; padding: 11px 14px; background: var(--brand); color: #fff; cursor: pointer;
}
.btn.secondary { background: #d6e4f0; color: var(--ink); }
.btn.danger { background: var(--danger); }
.btn.accent { background: var(--accent); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "header header"
    "sidebar content";
}
.topbar {
  grid-area: header; display: flex; align-items: center; gap: 12px;
  padding: 0 14px;
  padding-top: env(safe-area-inset-top);
  min-height: calc(56px + env(safe-area-inset-top));
  background: var(--brand); color: #fff; position: sticky; top: 0; z-index: 20;
}
.overlay {
  display: none;
  position: fixed;
  top: calc(56px + env(safe-area-inset-top));
  left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,.35); z-index: 25;
}

.ticket-list { display: grid; gap: 10px; }
.ticket-item { cursor: pointer; }
.ticket-item h3 { margin: 0; font-size: 1.02rem; }
.ticket-card-head {
  display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; margin-bottom: 8px;
}
.ticket-item .meta a { color: var(--brand-2); text-decoration: none; }
.mobile-only { display: none; }
.desktop-only { display: block; }
.topbar-title { font-weight: 650; flex: 1; opacity: 0.9; font-size: 0.95rem; }
.topbar-brand {
  font-weight: 750; font-size: 1.05rem; letter-spacing: -0.02em;
  white-space: nowrap; margin-right: 8px;
}
.topbar-user { display: flex; gap: 10px; align-items: center; font-size: 0.9rem; }
.icon-btn, .link-btn { background: transparent; border: 0; color: inherit; cursor: pointer; }
.icon-btn { display: none; font-size: 1.3rem; padding: 4px 8px; }

.sidebar {
  grid-area: sidebar;
  background: #16324a; color: #d5e6f2; padding: 12px 8px; overflow: auto;
}
.sidebar a, .sidebar button.nav-item {
  display: block; width: 100%; text-align: left; color: inherit; text-decoration: none;
  background: transparent; border: 0; border-radius: 10px; padding: 10px 12px; cursor: pointer;
}
.sidebar a.active, .sidebar button.nav-item.active, .sidebar a:hover, .sidebar button.nav-item:hover {
  background: rgba(255,255,255,0.1);
}
.nav-group { margin-top: 8px; }
.nav-group summary { padding: 10px 12px; cursor: pointer; color: #9cb9d0; }
.nav-sub { padding-left: 8px; display: grid; gap: 2px; }

.content { grid-area: content; min-width: 0; padding: 16px; overflow: auto; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; align-items: center; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; box-shadow: 0 1px 0 rgba(18,40,70,0.04);
}
.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; font-size: 0.92rem; }
th { background: #eef4fa; color: var(--muted); font-weight: 600; }
tr:hover td { background: #f5f9fc; }
.tickets-table .ticket-row { cursor: pointer; }
.tickets-table td, .tickets-table th { white-space: nowrap; }
.tickets-table td:nth-child(4) { white-space: normal; min-width: 140px; }
.cell-stack { display: grid; gap: 2px; line-height: 1.25; }
.cell-stack .muted { color: var(--muted); font-size: 0.85rem; }
.badge {
  display: inline-block; padding: 3px 8px; border-radius: 999px; font-size: 0.78rem; font-weight: 600;
  background: #e2eef7; color: var(--brand);
}
.badge.new { background: #e5f0fa; color: #1a5f8a; }
.badge.plan { background: #e8f4fc; color: #1e6a96; }
.badge.work { background: #d9f2e4; color: var(--ok); }
.badge.done { background: #e6ebf0; color: #4a5d6e; }
.badge.cancel { background: #fde8e6; color: var(--danger); }

.meta { color: var(--muted); font-size: 0.88rem; display: grid; gap: 2px; }

.week-nav { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; }
.calendar {
  display: grid; grid-template-columns: repeat(7, minmax(120px, 1fr)); gap: 8px;
}
.day-col {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; min-height: 180px; padding: 8px;
}
.day-col h4 { margin: 0 0 8px; font-size: 0.85rem; color: var(--muted); }
.slot, .cal-ticket {
  font-size: 0.78rem; padding: 6px; border-radius: 8px; margin-bottom: 6px; background: #eaf2f8;
}
.cal-ticket {
  background: #e8f2fa; border-left: 3px solid var(--accent); cursor: pointer;
}
.cal-ticket strong { display: block; font-size: 0.82rem; margin-bottom: 2px; }
.cal-ticket .cal-status { display: block; font-size: 0.72rem; opacity: 0.9; margin-bottom: 3px; }
.cal-ticket.new { background: #e5f0fa; border-left-color: #1a5f8a; }
.cal-ticket.new strong, .cal-ticket.new .cal-status { color: #1a5f8a; }
.cal-ticket.plan { background: #e8f4fc; border-left-color: #1e6a96; }
.cal-ticket.plan strong, .cal-ticket.plan .cal-status { color: #1e6a96; }
.cal-ticket.work { background: #d9f2e4; border-left-color: var(--ok); }
.cal-ticket.work strong, .cal-ticket.work .cal-status { color: var(--ok); }
.cal-ticket.done { background: #e6ebf0; border-left-color: #4a5d6e; }
.cal-ticket.done strong, .cal-ticket.done .cal-status { color: #4a5d6e; }
.cal-ticket.cancel { background: #fde8e6; border-left-color: var(--danger); }
.cal-ticket.cancel strong, .cal-ticket.cancel .cal-status { color: var(--danger); }
.status-legend {
  display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; align-items: center;
}
.status-legend .cal-ticket {
  margin: 0; cursor: default; padding: 4px 8px; font-size: 0.75rem; font-weight: 600;
}

.form-grid { display: grid; gap: 10px; }
.form-grid.two { grid-template-columns: 1fr 1fr; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; flex-wrap: wrap; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.tabs button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 7px 12px; cursor: pointer; }
.tabs button.active { background: var(--brand); color: #fff; border-color: var(--brand); }

.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: end center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(12, 28, 44, 0.45); }
.modal-panel {
  position: relative; width: min(560px, 100%); max-height: 92vh; overflow: auto;
  background: var(--surface); border-radius: 16px 16px 0 0; padding: 18px; box-shadow: var(--shadow);
}
.modal-panel.wide { width: min(720px, 100%); }
.modal-panel h2 { margin: 0 0 12px; font-size: 1.2rem; }
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: #16324a; color: #fff; padding: 10px 16px; border-radius: 999px; z-index: 60;
}

.ticket-modal-head {
  display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; margin-bottom: 16px;
}
.ticket-modal-num { font-size: 1.25rem; font-weight: 700; }
.ticket-modal-sub { color: var(--muted); font-size: 0.88rem; margin-top: 4px; }
.ticket-detail-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; margin-bottom: 16px;
}
.ticket-field {
  background: #f2f7fb; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
  display: grid; gap: 4px;
}
.ticket-field.full { grid-column: 1 / -1; }
.ticket-field .lbl { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.ticket-field .val { font-size: 0.98rem; font-weight: 600; word-break: break-word; }
.ticket-field .val a { color: var(--brand-2); text-decoration: none; }
.ticket-field.reject-box { background: #fde8e6; border-color: #f0b4ae; }
.ticket-field.was-support-box { background: #e8f4fc; border-color: #a8cde8; }
.ticket-actions { justify-content: flex-start; flex-wrap: wrap; }
.materials-list { margin: 6px 0 0; padding-left: 18px; font-weight: 500; }
.materials-list li { margin: 4px 0; }

.ticket-chat {
  border: 1px solid var(--line); border-radius: 12px; background: #f4f8fb;
  padding: 12px; margin-bottom: 14px;
}
.ticket-chat-title { font-weight: 700; margin-bottom: 10px; }
.ticket-chat-list {
  display: grid; gap: 10px; max-height: 260px; overflow: auto;
  padding-right: 4px; margin-bottom: 12px;
}
.chat-empty { color: var(--muted); font-size: 0.92rem; padding: 8px 0; }
.chat-msg {
  background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
}
.chat-msg.mine { border-color: #8fb8d4; background: #eaf4fb; }
.chat-meta {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  color: var(--muted); font-size: 0.78rem; margin-bottom: 6px;
}
.chat-meta b { color: var(--ink); font-size: 0.85rem; }
.chat-del {
  margin-left: auto; border: 0; background: transparent; color: var(--muted);
  cursor: pointer; font-size: 0.9rem; padding: 2px 6px; border-radius: 6px; line-height: 1;
}
.chat-del:hover { color: var(--danger); background: #fde8e6; }
.chat-body { white-space: pre-wrap; word-break: break-word; line-height: 1.4; }
.chat-photos {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 8px; margin-top: 8px;
}
.chat-photo {
  display: block; aspect-ratio: 1; border-radius: 8px; overflow: hidden;
  border: 1px solid var(--line); background: #d5e3ef;
}
.chat-photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ticket-chat-form textarea { resize: vertical; min-height: 64px; }
.chat-form-row {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 8px;
}
.file-btn {
  display: inline-flex; align-items: center; gap: 6px;
  background: #d6e4f0; color: var(--ink); border-radius: 10px; padding: 10px 12px; cursor: pointer;
  font-size: 0.92rem;
}
.chat-files-hint { color: var(--muted); font-size: 0.85rem; flex: 1; }
.logs-table td { vertical-align: top; font-size: 0.9rem; }
.log-details { max-width: 360px; word-break: break-word; font-weight: 400; }
.log-kv { white-space: nowrap; }
.muted { color: var(--muted); font-size: 0.85rem; }
.user-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.inline-label { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 0.9rem; }
.inline-label select { min-width: 180px; }
a.btn { display: inline-block; text-decoration: none; font-size: 0.85rem; padding: 7px 10px; }

.filters-panel { margin-bottom: 14px; }
.filters-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
}
.filters-grid label {
  display: grid; gap: 5px; font-size: 0.82rem; color: var(--muted);
}
.filter-count { color: var(--muted); font-size: 0.9rem; margin-left: auto; }
.check-label {
  display: flex !important; flex-direction: row; align-items: center; gap: 8px;
  align-self: end; padding-bottom: 10px;
}
.check-label input { width: auto; }
.billing-fields {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
@media (max-width: 560px) {
  .billing-fields { grid-template-columns: 1fr; }
}


@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "header"
      "content";
  }
  .icon-btn { display: inline-block; }
  .sidebar {
    position: fixed;
    top: calc(56px + env(safe-area-inset-top));
    left: 0; bottom: 0;
    width: min(280px, 86vw); z-index: 30;
    transform: translateX(-105%); transition: transform .2s ease;
  }
  .sidebar.open { transform: translateX(0); }
  .overlay.show { display: block; }
  .calendar { grid-template-columns: 1fr; }
  .form-grid.two { grid-template-columns: 1fr; }
  .ticket-detail-grid { grid-template-columns: 1fr; }
  .topbar-user span { display: none; }
  .topbar-title { display: none; }
  .topbar-brand { font-size: 0.95rem; }
  .content { padding: 12px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
  .mobile-only { display: grid; }
  .desktop-only { display: none !important; }
  .modal { place-items: end stretch; }
  .modal-panel {
    width: 100%;
    border-radius: 16px 16px 0 0;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }
}

@media (min-width: 901px) {
  .modal { place-items: center; }
  .modal-panel { border-radius: 16px; }
}

.pagination-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  padding: 12px 14px;
}
.pagination-limit {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.92rem;
}
.pagination-limit select {
  min-width: 72px;
}
.pagination-nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}
.pagination-info {
  color: var(--muted);
  font-size: 0.92rem;
  white-space: nowrap;
}
.pagination-nav .btn[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
}
