:root {
    --bg: #f5f6f8;
    --panel: #ffffff;
    --border: #e3e6eb;
    --text: #1f2933;
    --muted: #6b7785;
    --accent: #2f6fe4;
    --accent-soft: #e8f0fe;
    --danger: #c0392b;
    --ok: #1e8e5a;
    --radius: 8px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #14171c;
        --panel: #1d2127;
        --border: #2c323b;
        --text: #e6e9ee;
        --muted: #9aa5b1;
        --accent: #5b8cf0;
        --accent-soft: #223252;
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.45;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0 0 12px; }
h2 { font-size: 16px; margin: 0 0 10px; }

.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }

/* Barra laterale */
.sidebar {
    background: var(--panel);
    border-right: 1px solid var(--border);
    padding: 16px 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.brand { font-weight: 700; font-size: 16px; padding: 4px 8px 12px; }
.sidebar-title { color: var(--muted); font-size: 12px; text-transform: uppercase; padding: 12px 8px 4px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
    display: flex; align-items: center; gap: 8px;
    padding: 7px 8px; border-radius: 6px; color: var(--text);
}
.nav a:hover { background: var(--accent-soft); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.team-icon { width: 18px; text-align: center; }
.team-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { background: var(--accent); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; }
.sidebar-footer { margin-top: auto; border-top: 1px solid var(--border); padding-top: 12px; }
.me { margin-bottom: 6px; }

/* Area principale */
.main { padding: 20px; overflow-x: hidden; }
.card {
    background: var(--panel); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 16px; margin-bottom: 16px;
}
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Posta in arrivo: lista + thread */
.inbox { display: grid; grid-template-columns: 340px 1fr; gap: 16px; height: calc(100vh - 40px); }
.list-panel, .thread-panel {
    background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
    overflow: hidden; display: flex; flex-direction: column;
}
.list-head, .thread-head { padding: 12px; border-bottom: 1px solid var(--border); }
.tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.tabs a { padding: 4px 10px; border-radius: 6px; color: var(--muted); }
.tabs a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.search input { width: 100%; }
.conv-list { overflow-y: auto; flex: 1; }
.conv-item { display: block; padding: 10px 12px; border-bottom: 1px solid var(--border); color: var(--text); }
.conv-item:hover { background: var(--accent-soft); text-decoration: none; }
.conv-item.current { background: var(--accent-soft); }
.conv-top { display: flex; justify-content: space-between; gap: 8px; }
.conv-who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-preview { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.conv-time { color: var(--muted); font-size: 12px; white-space: nowrap; }

.thread-scroll { overflow-y: auto; padding: 16px; flex: 1; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 78%; padding: 10px 12px; border-radius: var(--radius); border: 1px solid var(--border); background: var(--panel); }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; background: var(--accent-soft); border-color: transparent; }
.msg.note { align-self: stretch; max-width: 100%; background: #fff8dc; border-color: #f0dfa0; color: #4a4120; }
.msg-meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg-body { white-space: pre-wrap; word-wrap: break-word; }
.msg-body.html { white-space: normal; }
.attachments { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 6px; }
.attachments a { font-size: 12px; border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px; }

.composer { border-top: 1px solid var(--border); padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.composer textarea { min-height: 90px; }
.composer-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.canned { display: flex; flex-wrap: wrap; gap: 6px; }
.canned button { font-size: 12px; }

.side-panel { padding: 12px; overflow-y: auto; }
.side-panel dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; }
.side-panel dt { color: var(--muted); }
.side-panel dd { margin: 0; word-break: break-all; }
.side-panel section { border-top: 1px solid var(--border); padding: 10px 0; }

/* Form e pulsanti */
input, select, textarea {
    font: inherit; color: var(--text); background: var(--panel);
    border: 1px solid var(--border); border-radius: 6px; padding: 7px 9px;
}
textarea { width: 100%; resize: vertical; }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 160px; }
.row > .shrink { flex: 0 0 auto; }
button, .button {
    font: inherit; cursor: pointer; border: 1px solid var(--border);
    background: var(--panel); color: var(--text); border-radius: 6px; padding: 7px 12px;
    display: inline-block;
}
button:hover, .button:hover { border-color: var(--accent); text-decoration: none; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--danger); }
button.link { border: none; background: none; color: var(--accent); padding: 0; }

.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { color: var(--muted); font-weight: 500; }

.tabs-settings { display: flex; gap: 6px; margin-bottom: 16px; flex-wrap: wrap; }
.tabs-settings a { padding: 6px 12px; border: 1px solid var(--border); border-radius: 6px; color: var(--text); }
.tabs-settings a.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.flash { padding: 10px 12px; border-radius: 6px; margin-bottom: 12px; border: 1px solid var(--border); }
.flash-success { background: #e6f6ee; border-color: #bfe5cf; color: #14623d; }
.flash-error { background: #fdecea; border-color: #f5c2bb; color: #8a2519; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat .value { font-size: 26px; font-weight: 700; }
.stat .label { color: var(--muted); }

.login-page { display: grid; place-items: center; min-height: 100vh; }
.login-card { width: 340px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }

/* Schermi piccoli */
@media (max-width: 860px) {
    .app { grid-template-columns: 1fr; }
    .sidebar { border-right: none; border-bottom: 1px solid var(--border); }
    .inbox { grid-template-columns: 1fr; height: auto; }
    .thread-panel { min-height: 70vh; }
    .msg { max-width: 92%; }
}

.inbox-3 { grid-template-columns: 340px 1fr 260px; }
@media (max-width: 1100px) {
    .inbox-3 { grid-template-columns: 300px 1fr; }
    .inbox-3 .side-panel { grid-column: 1 / -1; }
}

.mailbox-section {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 12px 12px;
    margin: 0;
}
.mailbox-section legend { padding: 0 6px; color: var(--muted); font-weight: 600; }

/* Pesi brand: dark navy sidebar with the white logo */
:root {
    --brand-navy: #1b2433;
    --brand-navy-hover: rgba(255, 255, 255, 0.08);
    --brand-navy-active: rgba(255, 255, 255, 0.14);
    --brand-text: #e9edf3;
    --brand-muted: #8d99ab;
}

.sidebar {
    background: var(--brand-navy);
    border-right: none;
    color: var(--brand-text);
}
.sidebar .brand { padding: 6px 8px 18px; }
.sidebar .brand img { display: block; }
.sidebar .sidebar-title { color: var(--brand-muted); }
.sidebar .nav a { color: var(--brand-text); }
.sidebar .nav a:hover { background: var(--brand-navy-hover); }
.sidebar .nav a.active { background: var(--brand-navy-active); color: #fff; }
.sidebar .muted { color: var(--brand-muted); }
.sidebar .sidebar-footer { border-top-color: rgba(255, 255, 255, 0.12); }
.sidebar button.link { color: var(--brand-text); }
.sidebar .badge { background: #3d7bff; }

.login-page { background: var(--brand-navy); }
.login-card { background: var(--brand-navy); border-color: rgba(255, 255, 255, 0.12); color: var(--brand-text); }
.login-card label { color: var(--brand-text); }
.login-card input { background: #111826; color: var(--brand-text); border-color: rgba(255, 255, 255, 0.16); }
.login-card button.primary { background: #3d7bff; border-color: #3d7bff; }
.login-logo { margin-bottom: 18px; }

.fetch-form { margin: 6px 8px 0; }
.fetch-button { width: 100%; background: transparent; color: var(--brand-text); border: 1px solid rgba(255, 255, 255, 0.25); }
.fetch-button:hover { background: var(--brand-navy-hover); border-color: #fff; }

.status-queued { color: #b7791f; font-weight: 600; }
.status-failed { color: var(--danger); font-weight: 600; }


/* Sidebar v2: icons, grouped sections, avatar and language switch */
.app { grid-template-columns: 236px 1fr; }
.sidebar { padding: 18px 12px; gap: 4px; }
.sidebar .brand { display: flex; flex-direction: column; gap: 4px; padding: 4px 10px 22px; }
.brand-sub { font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--brand-muted); }
.nav-item {
    display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
    color: var(--brand-text); border: none; background: none; font: inherit; cursor: pointer; width: 100%; text-align: left;
}
.nav-item:hover { background: var(--brand-navy-hover); text-decoration: none; }
.nav-item.active { background: var(--brand-navy-active); color: #fff; font-weight: 600; box-shadow: inset 3px 0 0 #3d7bff; }
.nav-item .icon { flex: 0 0 18px; opacity: .85; }
.nav-item.active .icon { opacity: 1; }
.nav-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar .sidebar-title { padding: 18px 10px 6px; font-size: 11px; letter-spacing: .08em; }
.team-item .team-icon { width: 22px; height: 22px; border-radius: 6px; background: rgba(255,255,255,.08); display: inline-flex; align-items: center; justify-content: center; font-size: 13px; }
.sidebar-hint { padding: 4px 10px; }
.sidebar .badge { background: #3d7bff; min-width: 22px; text-align: center; }
.sidebar-footer { display: flex; flex-direction: column; gap: 8px; }
.me { display: flex; align-items: center; gap: 10px; padding: 6px 4px; }
.me-text { display: flex; flex-direction: column; line-height: 1.25; }
.avatar {
    width: 34px; height: 34px; border-radius: 50%; background: #3d7bff; color: #fff;
    display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12px;
}
.lang-form { display: flex; align-items: center; gap: 8px; padding: 0 4px; }
.lang-icon { color: var(--brand-muted); display: inline-flex; }
.lang-form select { flex: 1; background: #111826; color: var(--brand-text); border-color: rgba(255,255,255,.16); padding: 5px 8px; }
.sidebar-footer .logout { color: var(--brand-muted); }
.sidebar-footer .logout:hover { color: #fff; }
.sidebar .muted { color: var(--brand-muted); }
.icon { display: inline-block; vertical-align: middle; }
button .icon { margin-right: 6px; }

/* Conversation view: collapsible messages, attachments, composer tools */
.ticket { font-size: 12px; color: var(--muted); font-weight: 500; margin-left: 6px; }
.msg-body.collapsible.is-collapsed { max-height: 220px; overflow: hidden; -webkit-mask-image: linear-gradient(to bottom, #000 70%, transparent); mask-image: linear-gradient(to bottom, #000 70%, transparent); }
.expand-toggle { align-self: flex-start; border: none; background: none; color: var(--accent); padding: 4px 0; font-size: 12px; cursor: pointer; }
.thumb img { max-width: 180px; max-height: 140px; border-radius: 6px; border: 1px solid var(--border); display: block; }
.thumb { display: inline-block; }

.composer-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.tools { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.tool {
    position: relative; display: inline-flex; align-items: center; justify-content: center;
    width: 34px; height: 34px; border-radius: 8px; border: 1px solid transparent; background: none;
    color: var(--muted); cursor: pointer; padding: 0;
}
.tool:hover { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.tool .icon { margin: 0; }
.file-input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }
.send-button { display: inline-flex; align-items: center; gap: 6px; }
.send-button .icon { margin: 0; }

.dropdown { position: relative; }
.dropdown-menu {
    display: none; position: absolute; bottom: 42px; left: 0; z-index: 20; min-width: 220px; max-width: 280px;
    background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 8px;
    box-shadow: 0 10px 30px rgba(0,0,0,.15);
}
.dropdown-menu.open { display: block; }
.menu-item { display: block; width: 100%; text-align: left; border: none; background: none; padding: 7px 8px; border-radius: 6px; }
.menu-item:hover { background: var(--accent-soft); }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; min-width: 260px; }
.emoji { border: none; background: none; font-size: 20px; padding: 4px; border-radius: 6px; cursor: pointer; }
.emoji:hover { background: var(--accent-soft); }
.schedule-menu { display: flex; flex-direction: column; gap: 8px; }

.history { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 10px; }
.history li { border-bottom: 1px solid var(--border); padding-bottom: 8px; }
.history a { font-weight: 600; }

/* Chat colours: customer, operator and internal notes */
:root { --msg-in: #eef1f6; --msg-out: #e2efff; --msg-note: #fff8cc; --msg-note-border: #f0e19a; }
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) { --msg-in: #2a313c; --msg-out: #1f3352; --msg-note: #4a4420; --msg-note-border: #6b5f2a; }
}
.msg.in { background: var(--msg-in); border-color: transparent; }
.msg.out { background: var(--msg-out); border-color: transparent; }
.msg.note { background: var(--msg-note); border-color: var(--msg-note-border); color: var(--text); }
.msg.note .msg-meta { color: #8a7a2a; }

/* Thread toolbar */
.thread-tools { display: flex; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; align-items: center; }
.thread-tools form { margin: 0; }
.tool-btn {
    display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 8px;
    border: 1px solid var(--border); background: var(--panel); color: var(--text); font-size: 13px; cursor: pointer;
}
.tool-btn:hover { border-color: var(--accent); }
.tool-btn.danger { color: var(--danger); }
.transcript-menu { min-width: 240px; left: 0; bottom: auto; top: 40px; }
.transcript-menu .radio { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.transcript-menu input[type="email"] { width: 100%; }

/* Composer tabs: reply / note */
.composer-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); padding: 0 4px; }
.ctab {
    display: inline-flex; align-items: center; gap: 6px; border: none; background: none; padding: 8px 12px;
    color: var(--muted); border-bottom: 2px solid transparent; border-radius: 0; cursor: pointer;
}
.ctab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.note-tab .icon { color: #b59b1a; }
.tab-panel[hidden] { display: none; }
.note-panel textarea { background: var(--msg-note); border-color: var(--msg-note-border); }

/* Conversation list rows with a quick-close icon */
.conv-row { display: flex; align-items: stretch; border-bottom: 1px solid var(--border); }
.conv-row.current { background: var(--accent-soft); }
.conv-row:hover { background: var(--accent-soft); }
.conv-row .conv-item { flex: 1; min-width: 0; border-bottom: none; }
.conv-row.current .conv-item, .conv-row:hover .conv-item { background: transparent; }
.conv-action { display: flex; align-items: center; padding-right: 8px; margin: 0; }
.icon-btn {
    width: 30px; height: 30px; display: inline-flex; align-items: center; justify-content: center;
    border-radius: 8px; border: 1px solid transparent; background: none; color: var(--muted); cursor: pointer; padding: 0;
}
.icon-btn:hover { color: var(--ok); border-color: var(--border); background: var(--panel); }
.icon-btn .icon { margin: 0; }


/* Personal inboxes, avatars and red counters */
.badge-red { background: #e5484d !important; color: #fff; }
.avatar-sm { width: 26px !important; height: 26px !important; font-size: 10px !important; flex: 0 0 26px; }
.avatar-lg { width: 96px !important; height: 96px !important; font-size: 30px !important; }
.avatar-img { object-fit: cover; display: inline-block; }
img.avatar-sm, img.avatar-lg { border-radius: 50%; }
.person-item .nav-label { font-weight: 500; }
.people .avatar { color: #fff; }

/* Sidebar sections (Team / Membri del team) and icon picker */
.sidebar-section { display: flex; justify-content: space-between; align-items: center; padding-right: 10px; }
.section-add { font-size: 16px; color: var(--brand-muted); }
.team-item .team-icon { width: 30px; height: 30px; border-radius: 8px; font-size: 16px; color: #1f2933; }
.count-pill { background: rgba(255,255,255,.12); color: var(--brand-text); border-radius: 12px; padding: 0 8px; font-size: 12px; min-width: 24px; text-align: center; }
.icon-picker { display: flex; flex-direction: column; gap: 10px; }
.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 4px; margin-top: 4px; }
.icon-choice { position: relative; }
.icon-choice input { position: absolute; opacity: 0; pointer-events: none; }
.icon-choice span { display: flex; align-items: center; justify-content: center; height: 40px; border-radius: 8px; border: 1px solid var(--border); font-size: 20px; cursor: pointer; background: var(--panel); }
.icon-choice:hover span { border-color: var(--accent); }
.icon-choice input:checked + span { border: 2px solid var(--accent); background: var(--accent-soft); }

/* Assignment popover (header), modelled on Customerly */
.tool-btn.icon-only { width: 38px; height: 38px; padding: 0; justify-content: center; }
.tool-btn.icon-only .icon { margin: 0; }
.assign-popover {
    bottom: auto; top: calc(100% + 8px); left: 0; right: auto;
    width: 300px; min-width: 0; max-width: none; padding: 0; overflow: hidden;
    border-radius: 14px; box-shadow: 0 16px 40px rgba(0,0,0,.18);
}
.assign-popover form { margin: 0; }
.assign-search {
    display: flex; flex-direction: row; align-items: center; gap: 10px; margin: 0; padding: 14px 16px;
    border-bottom: 1px solid var(--border); color: var(--muted);
}
.assign-search .icon { margin: 0; flex: 0 0 auto; }
.assign-search input {
    flex: 1; width: auto; min-width: 0; margin: 0; border: none; background: none; padding: 0; font-size: 15px; color: var(--text); outline: none; text-align: left;
}
.assign-list { max-height: 360px; overflow-y: auto; padding: 6px; }
.assign-item {
    display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
    border: none; background: none; padding: 9px 10px; border-radius: 10px; color: var(--text); font-size: 15px; cursor: pointer;
}
.assign-item:hover { background: var(--accent-soft); }
.assign-item .icon { margin: 0 0 0 auto; color: var(--accent); }
.assign-item .avatar { margin: 0; }
.assign-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.assign-none {
    display: inline-flex; align-items: center; justify-content: center; flex: 0 0 26px;
    width: 26px; height: 26px; border-radius: 50%; background: var(--border); color: var(--muted);
}
.assign-none .icon { margin: 0; width: 14px; height: 14px; }
.assignee-card { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.assignee-card .avatar { margin: 0; }

/* Composer menus stay closed until opened (overrides display rules set by menu variants) */
.dropdown-menu:not(.open) { display: none !important; }


/* Statistics dashboard */
.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip { padding: 4px 10px; border-radius: 14px; border: 1px solid var(--border); color: var(--text); font-size: 12px; }
.chip:hover { border-color: var(--accent); text-decoration: none; }
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; margin-bottom: 16px; }
.chart-card { margin-bottom: 0; }
.chart-box { position: relative; height: 280px; }
.table-scroll { overflow-x: auto; }
button.primary, a.button { white-space: nowrap; }

.quoted { margin-top: 6px; font-size: 13px; color: var(--muted); }
.quoted summary { cursor: pointer; color: var(--accent); }
.quoted .msg-body { margin-top: 6px; padding-left: 10px; border-left: 3px solid var(--border); }

.translate-menu { min-width: 240px; display: flex; flex-direction: column; gap: 8px; }
.translate-menu select { width: 100%; }

/* Delivery marks on outgoing messages */
.delivery { display: inline-flex; align-items: center; gap: 4px; }
.delivery .icon { width: 14px; height: 14px; }
.delivery.pending { color: #b7791f; font-weight: 600; }
.delivery.sent { color: var(--muted); }
.delivery.read { color: #1e8e5a; font-weight: 600; }
.delivery.failed { color: var(--danger); font-weight: 600; }


/* WhatsApp: channel label and notice toggle */
.via-wa { color: #1e8e5a; font-weight: 600; }
.wa-toggle { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; font-size: 12px; color: var(--muted); flex-direction: row; font-weight: 500; }
.wa-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.wa-switch { width: 34px; height: 20px; border-radius: 12px; background: #c9cfd8; position: relative; transition: background .15s; }
.wa-switch::after { content: ''; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: left .15s; }
.wa-toggle input:checked + .wa-switch { background: #25d366; }
.wa-toggle input:checked + .wa-switch::after { left: 16px; }

.msg-editor { display: flex; flex-direction: column; gap: 6px; }
.msg-editor textarea { width: 100%; }
.emoji-bar { display: flex; flex-wrap: wrap; gap: 2px; }
.emoji-bar .emoji { font-size: 18px; }

.order-row { border-bottom: 1px solid var(--border); padding: 6px 0; }
.paid { color: #1e8e5a; font-weight: 600; }
.unpaid { color: var(--danger); font-weight: 600; }


/* ===== Mobile web app ===== */
.m-body { margin: 0; background: var(--bg); min-height: 100vh; min-height: 100dvh; }
.m-top { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 10px; padding: calc(env(safe-area-inset-top) + 10px) 14px 10px; background: var(--panel); border-bottom: 1px solid var(--border); }
.m-title { font-size: 18px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-back, .m-icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 36px; border-radius: 50%; border: none; background: var(--accent-soft); color: var(--accent); font-size: 18px; cursor: pointer; text-decoration: none; }
.m-main { padding: 12px 12px calc(88px + env(safe-area-inset-bottom)); }
.m-tabs { position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: flex; justify-content: space-around; background: var(--panel); border-top: 1px solid var(--border); padding: 6px 0 calc(6px + env(safe-area-inset-bottom)); }
.m-tabs a { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--muted); text-decoration: none; flex: 1; }
.m-tabs a.active { color: var(--accent); }
.m-tabs .icon { width: 22px; height: 22px; }
.m-chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 8px; scrollbar-width: none; }
.m-chip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 20px; border: 1px solid var(--border); background: var(--panel); color: var(--text); white-space: nowrap; text-decoration: none; font-size: 14px; }
.m-chip.active { border-color: var(--accent); background: var(--accent-soft); }
.m-segment { display: flex; gap: 6px; overflow-x: auto; margin: 6px 0 10px; }
.m-segment a { padding: 6px 12px; border-radius: 8px; color: var(--muted); text-decoration: none; white-space: nowrap; font-size: 14px; }
.m-segment a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.m-search input { width: 100%; }
.m-list { display: flex; flex-direction: column; gap: 8px; }
.m-card { display: flex; gap: 12px; padding: 12px; border-radius: 14px; background: var(--panel); border: 1px solid var(--border); color: var(--text); text-decoration: none; align-items: center; }
.m-card-body { display: flex; flex-direction: column; min-width: 0; flex: 1; gap: 2px; }
.m-card-top { display: flex; justify-content: space-between; gap: 8px; }
.m-card-top time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.m-card-subject { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-card-preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-avatar { width: 40px !important; height: 40px !important; font-size: 14px !important; flex: 0 0 40px; }
.m-empty { text-align: center; padding: 24px 0; }
.m-group { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; margin-bottom: 12px; overflow: hidden; }
.m-group .m-row, .m-group form { display: flex; align-items: center; gap: 12px; padding: 13px 14px; border-bottom: 1px solid var(--border); color: var(--text); text-decoration: none; width: 100%; background: none; border-left: 0; border-right: 0; border-top: 0; font: inherit; text-align: left; }
.m-group > *:last-child { border-bottom: none; }
.m-group select { flex: 1; background: transparent; border: none; }
.m-me { display: flex; gap: 12px; align-items: center; padding: 14px; }
.m-thread-head { padding: 0 2px 10px; }
.m-thread-name { font-weight: 700; font-size: 16px; }
.m-messages { display: flex; flex-direction: column; gap: 10px; max-height: 62vh; overflow-y: auto; padding: 4px 0; }
.m-msg { max-width: 88%; padding: 10px 12px; border-radius: 14px; }
.m-msg.in { align-self: flex-start; background: var(--msg-in); }
.m-msg.out { align-self: flex-end; background: var(--msg-out); }
.m-msg.note { align-self: stretch; max-width: 100%; background: var(--msg-note); }
.m-msg .msg-meta { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.m-composer { margin-top: 12px; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 8px; }
.m-composer textarea { border: none; background: transparent; }
.m-composer-bar { display: flex; align-items: center; gap: 6px; padding-top: 6px; }
.m-tool { position: relative; width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel); font-size: 17px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; }
.m-tool input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; }
.m-send { margin-left: auto; }
.m-sheet { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.4); display: flex; align-items: flex-end; }
.m-sheet[hidden] { display: none; }
.m-sheet-panel { width: 100%; max-height: 80vh; overflow-y: auto; background: var(--panel); border-radius: 18px 18px 0 0; padding: 8px 14px calc(16px + env(safe-area-inset-bottom)); }
.m-sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 4px auto 10px; }
.m-sheet-panel h2 { font-size: 16px; }
.m-sheet-panel .m-row, .m-sheet-panel .m-row-form { display: flex; align-items: center; gap: 10px; width: 100%; padding: 13px 4px; border: none; border-bottom: 1px solid var(--border); background: none; color: var(--text); font: inherit; text-align: left; }
.m-sheet-panel .m-row-form select { flex: 1; }
.m-sheet-panel .danger { color: var(--danger); }
.m-emoji { display: grid; grid-template-columns: repeat(8, 1fr); padding: 6px 0 12px; }
.m-emoji .emoji { font-size: 24px; }
@media (min-width: 861px) { .m-tabs { max-width: 480px; left: 50%; transform: translateX(-50%); border-radius: 14px 14px 0 0; } }

.thread-subject { margin-top: 6px; font-size: 14px; font-weight: 600; }
.m-thread-subject { font-size: 14px; font-weight: 600; margin-top: 2px; }


/* Side panel: clear blocks, inline controls, full-width primary actions */
.side-panel { padding: 0; gap: 0; }
.side-block { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.side-block:last-child { border-bottom: none; }
.side-title { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
.side-name { font-size: 16px; font-weight: 700; line-height: 1.3; }
.side-meta { color: var(--muted); font-size: 13px; margin-top: 2px; word-break: break-all; }
.side-gap { margin-top: 10px; }
.side-label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.inline-form { display: flex; gap: 6px; align-items: center; margin: 0; }
.inline-form input, .inline-form select { flex: 1; min-width: 0; }
.inline-form button { white-space: nowrap; }
.side-block button, .side-block input, .side-block select { font-size: 13px; }
.btn-block { width: 100%; }
.side-block form { margin: 0; }
.side-block .orders-box button { width: 100%; }
.side-block .history { margin-top: 0; }

/* Emergency actions sit at the bottom of the side panel */
.emergency-block { border-bottom: none; }
.btn-block.danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-block.danger-solid:hover { filter: brightness(1.08); }

/* Customer avatar in the conversation header: Gravatar picture over initials */
.contact-row { display: flex; align-items: center; gap: 12px; }
.contact-text { min-width: 0; }
.contact-avatar-wrap { position: relative; width: 48px; height: 48px; flex: 0 0 48px; }
.contact-avatar { position: absolute; inset: 0; width: 48px !important; height: 48px !important; margin: 0; border-radius: 50%; font-size: 17px; }
.contact-avatar-img { object-fit: cover; background: var(--panel); }
.sort-link { color: inherit; text-decoration: none; white-space: nowrap; }
.sort-link:hover { color: var(--accent); }
