:root {
  --bg: #FBFBF8;
  --surface: #FFFFFF;
  --tint: #F5F5F1;
  --ink: #16191D;
  --body: #32373C;
  --muted: #5C6166;
  --border: #DEDDD5;
  --control: #8B9096;
  --accent: #C93F12;
  --accent-hover: #A93409;
  --accent-wash: #FBEEE8;
  --radius: 3px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang HK", "Microsoft JhengHei", sans-serif;
  --font-serif: Georgia, "Times New Roman", serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--body);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
  font-synthesis: none;
}

h1, h2, h3 {
  margin: 0;
  color: var(--ink);
  font-weight: 600;
}

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

button {
  cursor: pointer;
}

.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
}

.brand-lockup {
  margin-bottom: 20px;
}

.brand-kicker {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.04em;
  margin-bottom: 6px;
}

.brand-lockup h1 {
  font-family: var(--font-serif);
  font-size: 28px;
  line-height: 1.2;
  margin-bottom: 6px;
}

.brand-lockup p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.auth-footer {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-inner {
  max-width: 1360px;
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.topbar-brand {
  display: flex;
  flex-direction: column;
}

.brand-title {
  font-family: var(--font-serif);
  font-size: 20px;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.2;
}

.brand-sub {
  font-size: 13px;
  color: var(--muted);
}

.nav-tabs {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.nav-tab {
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 7px 13px;
  font-weight: 500;
  color: var(--body);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.nav-tab:hover {
  background: var(--tint);
}

.nav-tab.active {
  background: var(--accent-wash);
  border-color: var(--accent);
  color: var(--accent);
}

.badge,
.chip-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  font-size: 13px;
  font-weight: 600;
  border-radius: var(--radius);
  background: var(--accent);
  color: #FFFFFF;
}

.chip:not(.active) .chip-count {
  background: var(--tint);
  color: var(--ink);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-pill {
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  padding: 4px 10px;
  background: var(--tint);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.main-wrap {
  max-width: 1360px;
  margin: 0 auto;
  padding: 20px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.filter-group {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.chip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 11px;
  font-size: 14px;
  color: var(--body);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.chip:hover {
  background: var(--tint);
}

.chip.active {
  background: var(--ink);
  border-color: var(--ink);
  color: #FFFFFF;
}

.search-input,
.field input,
.field select,
.field textarea,
.reply-box textarea,
.inline-field select,
.status-select {
  background: var(--surface);
  border: 1px solid var(--control);
  border-radius: var(--radius);
  padding: 8px 10px;
  font-size: 14px;
  line-height: 1.4;
}

.search-input {
  min-width: 240px;
}

.search-input:focus,
.field input:focus,
.field select:focus,
.field textarea:focus,
.reply-box textarea:focus,
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn {
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 8px 14px;
  font-weight: 500;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 13px;
}

.btn-primary {
  background: var(--accent);
  color: #FFFFFF;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface);
  border-color: var(--border);
  color: var(--ink);
}

.btn-secondary:hover {
  background: var(--tint);
}

.btn-danger {
  background: var(--surface);
  border-color: var(--accent);
  color: var(--accent);
}

.btn-danger:hover {
  background: var(--accent-wash);
}

.btn-link {
  background: none;
  border: none;
  padding: 4px 6px;
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Inbox Layout */
.inbox-grid {
  display: grid;
  grid-template-columns: 360px 1fr;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  min-height: 620px;
}

.thread-list-col {
  border-right: 1px solid var(--border);
  overflow-y: auto;
  max-height: 760px;
}

.thread-item {
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border);
  padding: 13px 14px;
  display: block;
}

.thread-item:hover {
  background: var(--tint);
}

.thread-item.selected {
  background: var(--accent-wash);
  border-left: 3px solid var(--accent);
  padding-left: 11px;
}

.thread-item.unread .thread-subj {
  font-weight: 700;
  color: var(--ink);
}

.thread-row-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.thread-sender {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-time {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}

.thread-subj {
  font-size: 14px;
  color: var(--body);
  margin-bottom: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-snip {
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thread-badges {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-top: 6px;
}

.mailbox-tag,
.supplier-tag,
.status-tag {
  display: inline-block;
  padding: 2px 7px;
  font-size: 13px;
  font-weight: 600;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--tint);
  color: var(--ink);
}

.mailbox-tag.trade {
  border-color: var(--accent);
  background: var(--accent-wash);
  color: var(--accent);
}

.mailbox-tag.info {
  background: var(--tint);
  color: var(--ink);
}

.unread-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  display: inline-block;
}

.thread-detail-col {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.empty-state {
  padding: 48px 24px;
  text-align: center;
  color: var(--muted);
}

.thread-content {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.thread-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
}

.thread-meta-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  flex-wrap: wrap;
}

.detail-subject {
  font-family: var(--font-serif);
  font-size: 22px;
  line-height: 1.25;
}

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

.message-list {
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex: 1;
  overflow-y: auto;
  max-height: 460px;
  background: var(--bg);
}

.message-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.message-card.outbound {
  border-left: 3px solid var(--accent);
}

.message-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.message-from {
  font-weight: 600;
  color: var(--ink);
  font-size: 14px;
}

.message-meta {
  font-size: 13px;
  color: var(--muted);
}

.message-body-text {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.6;
  color: var(--body);
}

.message-html-toggle {
  margin-top: 8px;
}

.message-html-frame {
  width: 100%;
  min-height: 260px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #FFFFFF;
  margin-top: 8px;
}

.attachment-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.attachment-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--tint);
  color: var(--ink);
  text-decoration: none;
  font-size: 13px;
}

.attachment-link:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.reply-box {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.reply-bar {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  align-items: center;
}

.inline-field {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
}

.reply-box textarea {
  width: 100%;
  resize: vertical;
}

.reply-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

/* Tables & Stats */
.stats-strip {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  margin-bottom: 16px;
}

.stat-item {
  padding: 14px 18px;
  border-right: 1px solid var(--border);
}

.stat-item:last-child {
  border-right: none;
}

.stat-label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 4px;
}

.stat-value {
  font-family: var(--font-serif);
  font-size: 26px;
  color: var(--ink);
}

.table-wrap {
  border: 1px solid var(--border);
  background: var(--surface);
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 14px;
}

.data-table th,
.data-table td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.data-table th {
  background: var(--tint);
  font-weight: 600;
  color: var(--ink);
  font-size: 13px;
}

.data-table tr:last-child td {
  border-bottom: none;
}

.cell-title {
  font-weight: 600;
  color: var(--ink);
}

.cell-sub {
  font-size: 13px;
  color: var(--muted);
}

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

/* Settings / Templates */
.two-col-settings {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 16px;
  align-items: start;
}

.panel-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}

.section-heading {
  font-family: var(--font-serif);
  font-size: 22px;
  margin-bottom: 12px;
}

.templates-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.template-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--bg);
}

.template-card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

/* Forms & Modals */
.stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
}

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

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

.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  width: min(680px, calc(100vw - 24px));
  background: var(--surface);
  color: var(--body);
}

.modal::backdrop {
  background: rgba(22, 25, 29, 0.45);
}

.modal-card {
  padding: 20px;
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.spacer {
  flex: 1;
}

.form-error {
  margin: 0;
  color: var(--accent);
  font-size: 13px;
  font-weight: 500;
}

.status-text {
  font-size: 13px;
  color: var(--muted);
}

.muted-text {
  font-size: 13px;
  color: var(--muted);
}

@media (max-width: 960px) {
  .inbox-grid {
    grid-template-columns: 1fr;
  }
  .thread-list-col {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 280px;
  }
  .two-col-settings {
    grid-template-columns: 1fr;
  }
  .grid-2,
  .grid-3,
  .stats-strip {
    grid-template-columns: 1fr;
  }
  .stat-item {
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .stat-item:last-child {
    border-bottom: none;
  }
}
