:root {
  --bg: #f6f7f4;
  --panel: #ffffff;
  --ink: #17201b;
  --muted: #68736d;
  --line: #dfe5df;
  --brand: #128c7e;
  --brand-dark: #075e54;
  --warn: #b7791f;
  --ok: #2f855a;
  --bad: #c53030;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 260px 1fr;
  color: var(--ink);
  background: var(--bg);
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.app-locked {
  display: block;
}

body.app-locked .sidebar,
body.app-locked main {
  display: none;
}

button, input, select, textarea {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  border-radius: 6px;
  padding: 9px 12px;
  cursor: pointer;
}

button.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: white;
}

button.link-button {
  border: 0;
  background: transparent;
  color: var(--brand-dark);
  padding: 6px 0;
  text-align: left;
  font-weight: 700;
}

button.success {
  background: var(--ok);
  border-color: var(--ok);
  color: white;
}

button:disabled {
  opacity: .5;
  cursor: not-allowed;
}

.sidebar {
  background: var(--brand-dark);
  color: white;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.sidebar h1 {
  font-size: 22px;
  margin: 0 0 6px;
}

.sidebar p {
  color: #cce7e2;
  margin: 0;
}

.sidebar p.connected {
  color: #d7fff2;
}

.sidebar p.warning {
  color: #ffe2bd;
}

.sidebar nav {
  display: grid;
  gap: 8px;
}

.nav {
  text-align: left;
  border-color: rgba(255,255,255,.18);
  background: transparent;
  color: white;
}

.nav.active {
  background: rgba(255,255,255,.16);
}

.sidebar label {
  display: grid;
  gap: 7px;
  color: #d8f0ec;
}

.sidebar select {
  width: 100%;
  border: 0;
  border-radius: 6px;
  padding: 10px;
}

main {
  min-width: 0;
  padding: 22px;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 18px;
}

.top-bar {
  display: block;
}

.refresh-button {
  min-width: 96px;
  background: #ffffff;
}

.dashboard {
  display: grid;
  grid-template-columns: minmax(220px, .8fr) minmax(220px, .8fr) minmax(420px, 1.6fr);
  gap: 12px;
}

.dashboard-toolbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f8fafc;
}

.dashboard-toolbar div {
  display: grid;
  gap: 2px;
}

.dashboard-toolbar span {
  color: var(--muted);
  font-size: 13px;
}

.metric-group {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
}

.metric-group.inbox {
  background: #eaf8ef;
  cursor: pointer;
}

.metric-group.inbox:hover {
  border-color: var(--brand);
  box-shadow: 0 8px 20px rgba(18, 140, 126, .12);
}

.metric-group.email {
  background: #edf5ff;
  cursor: pointer;
}

.metric-group.email:hover {
  border-color: #3182ce;
  box-shadow: 0 8px 20px rgba(49, 130, 206, .12);
}

.metric-group.tickets {
  background: #fff1df;
  cursor: pointer;
}

.metric-group.tickets:hover {
  border-color: #dd6b20;
  box-shadow: 0 8px 20px rgba(221, 107, 32, .14);
}

.notification-diagnostics {
  background: #f7faf9;
  border: 1px solid #b7ccc6;
  border-radius: 10px;
  grid-column: 1 / -1;
  padding: 16px;
}

.notification-diagnostics h2 {
  font-size: 18px;
  margin-bottom: 12px;
}

.notification-list {
  display: grid;
  gap: 10px;
}

.notification-row {
  background: #ffffff;
  border: 1px solid #d8e2df;
  border-left: 6px solid #64748b;
  border-radius: 8px;
  padding: 12px;
}

.notification-row.sent {
  border-left-color: #0f766e;
}

.notification-row.failed {
  border-left-color: #dc2626;
}

.notification-row.skipped {
  border-left-color: #ca8a04;
}

.notification-row div {
  align-items: baseline;
  display: flex;
  gap: 12px;
  justify-content: space-between;
}

.notification-row span,
.notification-row small {
  color: var(--muted);
}

.notification-row p {
  margin: 8px 0 4px;
}

.metric-group h2 {
  margin: 0 0 10px;
  font-size: 15px;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(120px, 1fr));
  gap: 10px;
}

.metric-group.tickets .metric-grid {
  grid-template-columns: repeat(3, minmax(120px, 1fr));
}

.metric {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.metric strong {
  display: block;
  font-size: 26px;
  line-height: 1.1;
}

.metric span {
  display: block;
  margin-top: 4px;
  font-weight: 700;
}

.metric small {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(330px, 420px) minmax(0, 1fr);
  gap: 16px;
  padding: 12px;
  border-radius: 10px;
}

#inboxView {
  background: #cfeedd;
}

#emailView {
  background: #d4e7fb;
}

#ticketsView {
  background: #f5d7b3;
}

.settings-workspace {
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  overflow: auto;
}

.hidden { display: none; }

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 22px;
  background: #eaf8ef;
}

.login-screen.hidden {
  display: none;
}

.login-panel {
  width: min(420px, 100%);
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
}

.login-panel form {
  display: grid;
  gap: 14px;
}

.login-panel h1 {
  margin: 0;
  font-size: 24px;
}

.login-panel p {
  margin: 0;
  color: var(--muted);
}

.login-panel label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}

#logoutButton {
  border-color: rgba(255,255,255,.22);
  background: rgba(255,255,255,.08);
  color: white;
}

.list-pane, .detail-pane {
  min-height: 0;
  background: #eef2f5;
  border: 1px solid #a8b3bd;
  border-radius: 8px;
  overflow: hidden;
}

.pane-head {
  padding: 16px;
  display: grid;
  gap: 12px;
  border-bottom: 1px solid #a8b3bd;
  background: #f8fafc;
}

.pane-head h2 {
  margin: 0;
  font-size: 18px;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  background: white;
}

textarea {
  min-height: 84px;
  resize: vertical;
}

.list {
  max-height: calc(100vh - 150px);
  min-height: calc(100vh - 250px);
  overflow: auto;
  padding-bottom: 14px;
  background: #e8edf2;
}

#emailView .list {
  max-height: calc(100vh - 215px);
  min-height: calc(100vh - 285px);
}

.email-actions,
.ticket-actions,
.email-import {
  padding: 12px 16px;
  border-bottom: 1px solid #a8b3bd;
  background: #eef5fc;
}

.email-actions,
.ticket-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.email-config-status {
  margin: 12px 16px 14px;
  padding: 10px 12px;
  border: 1px solid #a8b3bd;
  border-radius: 8px;
  background: #f8fbff;
  display: grid;
  gap: 3px;
  font-size: 13px;
}

.email-config-status.connected {
  border-color: #8fddad;
  background: #eefaf6;
  color: #14532d;
}

.email-config-status.warning {
  border-color: #fed7aa;
  background: #fff7ed;
  color: #7c2d12;
}

.email-config-status span {
  color: inherit;
  opacity: 0.85;
}

.email-import form {
  display: grid;
  gap: 10px;
}

.item {
  width: 100%;
  display: grid;
  gap: 5px;
  padding: 14px 16px;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  text-align: left;
  background: white;
}

.item.active {
  background: #edf8f6;
}

.item-top {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.item-title {
  font-weight: 700;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-meta, .item-preview {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#emailList .item {
  gap: 7px;
}

#emailList .item-top {
  align-items: flex-start;
}

#emailList .item-title {
  line-height: 1.28;
  overflow: visible;
  text-overflow: clip;
  white-space: normal;
  word-break: break-word;
}

#emailList .badge {
  flex: 0 0 auto;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  border-radius: 999px;
  padding: 2px 8px;
  background: #edf2f7;
  color: #2d3748;
  font-size: 12px;
  font-weight: 700;
}

.badge.new { background: #ebf8ff; color: #2b6cb0; }
.badge.open { background: #e6fffa; color: #08756b; }
.badge.waiting,
.badge.waiting-for-customer { background: #fffaf0; color: var(--warn); }
.badge.resolved { background: #f0fff4; color: var(--ok); }
.badge.closed { background: #f7fafc; color: #4a5568; }
.badge.unread { background: #d8f5ef; color: var(--brand-dark); }
.badge.priority-urgent { background: #fed7d7; color: #9b2c2c; }
.badge.priority-high { background: #feebc8; color: #9c4221; }
.badge.priority-normal { background: #e6fffa; color: #08756b; }
.badge.priority-low { background: #edf2f7; color: #4a5568; }

.detail-pane {
  display: grid;
  grid-template-rows: auto 1fr auto;
}

#emailDetail:not(.empty) {
  display: block;
  overflow: auto;
}

.detail-pane.empty {
  display: grid;
  place-items: center;
  color: var(--muted);
}

.detail-head {
  padding: 16px;
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 360px);
  align-items: start;
  gap: 16px;
}

.detail-head h2 {
  margin: 0;
  font-size: 20px;
  line-height: 1.25;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.filters {
  display: grid;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid #a8b3bd;
  background: #eef5fc;
}

.filter-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: start;
  padding-bottom: 2px;
}

.filter-group > span {
  flex: 0 0 72px;
  padding-top: 12px;
  color: #4a5568;
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.filter.active {
  background: var(--brand-dark);
  color: white;
  border-color: var(--brand-dark);
}

.messages, .ticket-body {
  padding: 16px;
  overflow: auto;
}

.messages {
  background: #dfe7df;
  scroll-behavior: smooth;
  min-height: 560px;
  max-height: calc(100vh - 300px);
}

.create-ticket-form,
.linked-ticket {
  border-bottom: 1px solid #a8b3bd;
  background: #e7edf2;
  padding: 16px;
}

.create-ticket-form {
  display: grid;
  gap: 12px;
}

.form-grid {
  display: grid;
  gap: 12px;
}

.form-grid.two-column {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.form-grid .wide {
  grid-column: 1 / -1;
}

.form-grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}

.form-grid input,
.form-grid textarea {
  color: var(--ink);
  font-weight: 400;
  background: #ffffff;
}

.create-ticket-form textarea[name="description"],
.manual-ticket-form textarea[name="description"] {
  min-height: 170px;
}

.helper {
  color: var(--muted);
  font-size: 12px;
}

.message {
  max-width: 72%;
  width: fit-content;
  padding: 10px 12px;
  margin-bottom: 10px;
  border-radius: 8px;
  background: #ffffff;
  border: 1px solid #b7c1cc;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
}

.message.outgoing {
  margin-left: auto;
  background: #d9fdd3;
  border-color: #8fddad;
}

.message small {
  display: block;
  color: var(--muted);
  margin-top: 4px;
}

.attachments {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.attachment {
  display: grid;
  gap: 3px;
  max-width: 280px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255,255,255,.78);
  color: var(--ink);
  padding: 9px;
  text-decoration: none;
}

.attachment span {
  color: var(--muted);
  font-size: 12px;
}

.image-attachment img {
  display: block;
  width: 100%;
  max-height: 220px;
  object-fit: contain;
  border-radius: 6px;
  background: #fff;
}

.attachment.unavailable {
  opacity: .75;
}

.detail-summary {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.info-card {
  border: 1px solid #a8b3bd;
  border-radius: 8px;
  background: #ffffff;
  padding: 12px;
}

.info-card.wide {
  grid-column: 1 / -1;
}

.info-card strong {
  display: block;
  margin-bottom: 5px;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
}

.info-card span {
  display: block;
}

.composer {
  padding: 16px;
  border-top: 1px solid #a8b3bd;
  background: #edf2f7;
  display: grid;
  gap: 10px;
}

.inline-composer {
  margin-top: 14px;
  padding: 14px 0 0;
}

.reply-channels {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 0.9fr);
  gap: 14px;
  margin-top: 14px;
}

.reply-channel {
  border: 1px solid #8ea2b2;
  border-radius: 8px;
  background: #f7fafc;
  padding: 14px;
}

.reply-channel h4,
.customer-email-box h4,
.email-reply-box h4 {
  margin: 0 0 10px;
  font-size: 16px;
}

.email-channel-box {
  display: grid;
  gap: 12px;
  background: #edf7f2;
}

.customer-email-box,
.email-reply-box {
  display: grid;
  gap: 10px;
}

.quick-reply {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}

.quick-reply select {
  color: var(--ink);
  font-weight: 400;
}

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.section {
  border: 1px solid #9aa8b5;
  border-radius: 8px;
  padding: 16px;
  margin-top: 16px;
  background: #e8edf2;
}

.section:first-child {
  margin-top: 0;
}

.section-details {
  background: #dbe9e5;
  border-left: 5px solid #7db8ad;
}

.section-notes {
  background: #dcebd4;
  border-left: 5px solid var(--ok);
}

.section-whatsapp {
  background: #d3eee5;
  border-left: 5px solid var(--brand);
}

.section-email {
  background: #d7e7f8;
  border-left: 5px solid #3182ce;
}

.section-email-thread {
  background: #d7e7f8;
  border-left: 5px solid #3182ce;
}

.section-email-reply {
  background: #d3eee5;
  border-left: 5px solid var(--brand);
}

.section-customer-email {
  background: #d3eee5;
  border-left: 5px solid var(--brand);
}

.email-reply-form {
  display: grid;
  gap: 12px;
}

.reply-context {
  display: grid;
  gap: 8px;
  border: 1px solid #8fb3c9;
  border-radius: 8px;
  background: #e8f3fb;
  padding: 10px;
}

.reply-context-meta {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 13px;
}

.reply-context-body {
  max-height: 150px;
  overflow: auto;
  white-space: pre-wrap;
  border: 1px solid #b9c7d3;
  border-radius: 6px;
  background: #ffffff;
  padding: 10px;
}

.email-reply-form label,
.inline-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}

.email-reply-form input,
.email-reply-box textarea,
.inline-form input {
  width: 100%;
}

.inline-form {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto;
  gap: 10px;
  align-items: end;
}

.email-reply-box textarea {
  min-height: 190px;
  background: #fff;
}

.email-reply-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 10px;
}

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  background: #fff;
  font-weight: 650;
}

.section-manual {
  background: #f1ddc3;
  border-left: 5px solid #dd6b20;
}

.section-internal {
  background: #e9ddf7;
  border-left: 5px solid #7c3aed;
}

.section-log {
  background: #dfe5eb;
  border-left: 5px solid #94a3b8;
}

.section h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.section-head h3 {
  margin: 0;
}

.settings-panel {
  padding: 18px;
}

.manual-ticket-workspace {
  max-width: 980px;
}

.manual-ticket-form {
  display: grid;
  gap: 16px;
  padding: 18px 0 0;
}

.settings-form {
  display: grid;
  gap: 16px;
  max-width: 980px;
}

.settings-section {
  border-top: 1px solid var(--line);
  padding-top: 16px;
}

.settings-section h3 {
  margin: 0 0 8px;
  font-size: 16px;
}

.settings-list {
  display: grid;
  gap: 10px;
}

.settings-list label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}

.settings-list textarea {
  min-height: 76px;
}

.settings-user-list {
  display: grid;
  gap: 12px;
}

.settings-user-row {
  display: grid;
  grid-template-columns: minmax(130px, 1fr) minmax(200px, 1.3fr) minmax(170px, 1fr) minmax(120px, 0.7fr) auto minmax(150px, 0.8fr);
  gap: 10px;
  align-items: end;
  padding: 12px;
  border: 1px solid #a8b3bd;
  border-radius: 8px;
  background: #ffffff;
}

.settings-user-row label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}

.settings-user-row .inline-check {
  display: flex;
  align-items: center;
  min-height: 42px;
}

.settings-user-actions {
  display: grid;
  gap: 6px;
  align-content: end;
}

.settings-user-actions span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.user-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.user-pills span {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #f7faf7;
  padding: 6px 10px;
  font-weight: 700;
}

.settings-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.save-state {
  color: var(--ok);
  font-weight: 700;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.reset-link-box {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid #7bbd9b;
  border-radius: 8px;
  background: #eefaf3;
  color: #12372b;
}

.reset-link-box span {
  display: block;
  margin-bottom: 6px;
  font-weight: 800;
}

.reset-link-box a {
  display: block;
  color: #005f49;
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.log-row, .note-row {
  padding: 10px 12px;
  border: 1px solid #a8b3bd;
  border-radius: 6px;
  background: #ffffff;
  margin-top: 8px;
}

.section-whatsapp .messages {
  border: 1px solid #9aa8b5;
  border-radius: 8px;
  margin-top: 10px;
  background: #c9ded7;
  min-height: 620px;
  max-height: calc(100vh - 260px);
}

.section-email-thread .messages {
  border: 1px solid #9aa8b5;
  border-radius: 8px;
  margin-top: 10px;
  background: #dce6f0;
  min-height: 620px;
  max-height: calc(100vh - 260px);
}

.section-whatsapp .inline-composer {
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
}

.email-message-body {
  white-space: pre-wrap;
  border: 1px solid #9aa8b5;
  border-radius: 8px;
  background: #ffffff;
  padding: 14px;
  max-height: 420px;
  overflow: auto;
}

.error {
  color: var(--bad);
}

@media (max-width: 1200px) {
  .reply-channels {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 900px) {
  body {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: static;
  }
  .top-bar, .dashboard, .workspace {
    grid-template-columns: 1fr;
  }
  .settings-user-row {
    grid-template-columns: 1fr;
  }
  .detail-head {
    grid-template-columns: 1fr;
  }
  .list {
    max-height: 360px;
  }
  .messages,
  .section-whatsapp .messages,
  .section-email-thread .messages {
    min-height: 420px;
    max-height: 60vh;
  }
}
