:root {
  --green: #008069;
  --green-dark: #005c4b;
  --accent: #25d366;
  --blue: #0866ff;
  --bg: #f0f2f5;
  --surface: #ffffff;
  --text: #111b21;
  --muted: #667781;
  --border: #e3e6ea;
  --chat-bg: #efeae2;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --chip: #ffffff;
  --ns-bg: #eef1f4;  --ns-fg: #54656f;
  --wk-bg: #fff4d6;  --wk-fg: #8a5a00;
  --cp-bg: #dcf8e6;  --cp-fg: #0b7a3e;
  --shadow: 0 1px 2px rgba(11, 20, 26, .13);
}
@media (prefers-color-scheme: dark) {
  :root {
    --green: #1f2c34;
    --green-dark: #111b21;
    --bg: #0b141a;
    --surface: #111b21;
    --text: #e9edef;
    --muted: #8696a0;
    --border: #222d34;
    --chat-bg: #0b141a;
    --bubble-in: #202c33;
    --bubble-out: #005c4b;
    --chip: #202c33;
    --ns-bg: #2a3942;  --ns-fg: #aebac1;
    --wk-bg: #3d3214;  --wk-fg: #f5c451;
    --cp-bg: #12392a;  --cp-fg: #5fd88f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
button, input, select { font: inherit; color: inherit; }
.hidden { display: none !important; }

.app {
  max-width: 1100px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  box-shadow: 0 0 20px rgba(0,0,0,.08);
}

/* ---------- top bar ---------- */
.topbar {
  background: var(--green);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 20px;
  flex-shrink: 0;
}
.brand-name { font-weight: 600; font-size: 17px; }
.brand-sub { font-size: 12.5px; opacity: .85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.switch {
  display: flex;
  background: rgba(255,255,255,.15);
  border-radius: 999px;
  padding: 3px;
  flex-shrink: 0;
}
.switch-btn {
  border: 0; background: transparent; color: #fff;
  padding: 7px 14px; border-radius: 999px; cursor: pointer; font-weight: 600;
}
.switch-btn.active { background: #fff; color: var(--green-dark); }
@media (prefers-color-scheme: dark) { .switch-btn.active { color: #111b21; } }

.view { display: none; flex: 1; min-height: 0; }
.view.active { display: flex; flex-direction: column; }

/* ---------- table view ---------- */
#tableView { padding: 16px; gap: 12px; overflow-y: auto; }
.card { background: var(--surface); border-radius: 10px; box-shadow: var(--shadow); }

.form {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; padding: 14px;
}
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); font-weight: 600; }
.form label.grow { flex: 1 1 240px; }
.form input, .form select, .filters input, .filters select, .cell-select {
  border: 1px solid var(--border); background: var(--surface);
  border-radius: 8px; padding: 8px 10px; min-height: 38px;
}
.form input:focus, .form select:focus, .filters input:focus, .filters select:focus, .composer input:focus {
  outline: 2px solid var(--accent); outline-offset: -1px;
}
.form-actions { display: flex; gap: 8px; }

.btn { border: 0; border-radius: 8px; padding: 9px 16px; cursor: pointer; font-weight: 600; min-height: 38px; }
.btn.primary { background: var(--green); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { background: var(--accent); color: #111b21; } }
.btn.ghost { background: transparent; border: 1px solid var(--border); }

.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters input { flex: 1 1 200px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tbody tr:hover { background: rgba(0,128,105,.05); }
td.id { color: var(--muted); font-variant-numeric: tabular-nums; }
td.date { white-space: nowrap; font-variant-numeric: tabular-nums; }
td.date.overdue { color: #d93025; font-weight: 600; }
tr.done td.work { text-decoration: line-through; color: var(--muted); }
.cell-select { min-height: 32px; padding: 4px 8px; font-size: 13.5px; }
.cell-select.st-not-started { background: var(--ns-bg); color: var(--ns-fg); border-color: transparent; }
.cell-select.st-working     { background: var(--wk-bg); color: var(--wk-fg); border-color: transparent; }
.cell-select.st-complete    { background: var(--cp-bg); color: var(--cp-fg); border-color: transparent; }
.row-actions { white-space: nowrap; text-align: right; }
.icon-btn { border: 0; background: transparent; cursor: pointer; padding: 6px 8px; border-radius: 6px; font-size: 16px; }
.icon-btn:hover { background: var(--bg); }
.empty { padding: 24px; text-align: center; color: var(--muted); margin: 0; }

/* ---------- chat view ---------- */
#chatView { background: var(--chat-bg); }
.chat-scroll {
  flex: 1; overflow-y: auto; padding: 16px 6%;
  background-image: radial-gradient(rgba(0,0,0,.035) 1px, transparent 1px);
  background-size: 18px 18px;
}
.chat-day {
  margin: 0 auto 12px; width: max-content; font-size: 12.5px;
  background: var(--surface); color: var(--muted); padding: 5px 12px; border-radius: 8px; box-shadow: var(--shadow);
}
.msg { display: flex; margin: 4px 0; }
.msg.out { justify-content: flex-end; }
.bubble {
  max-width: min(78%, 620px);
  padding: 7px 10px 6px; border-radius: 8px;
  background: var(--bubble-in); box-shadow: var(--shadow);
  white-space: pre-wrap; word-wrap: break-word; line-height: 1.4;
}
.msg.in .bubble { border-top-left-radius: 0; }
.msg.out .bubble { background: var(--bubble-out); border-top-right-radius: 0; }
.time { display: block; text-align: right; font-size: 11px; color: var(--muted); margin-top: 2px; }
.msg.out .time::after { content: " ✓✓"; color: #53bdeb; }

.tlist { list-style: none; padding: 0; margin: 8px 0 2px; display: flex; flex-direction: column; gap: 6px; white-space: normal; }
.titem { border-left: 3px solid var(--accent); background: rgba(0,0,0,.035); border-radius: 6px; padding: 6px 8px; }
@media (prefers-color-scheme: dark) { .titem { background: rgba(255,255,255,.05); } }
.titem.complete { border-left-color: var(--cp-fg); }
.titem.working  { border-left-color: var(--wk-fg); }
.titem.not-started { border-left-color: var(--ns-fg); }
.titem-top { display: flex; gap: 6px; align-items: baseline; }
.titem-id { color: var(--muted); font-size: 12.5px; }
.titem-work { font-weight: 600; }
.titem-meta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 4px; font-size: 12px; color: var(--muted); }
.pill { padding: 1px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; }
.pill.not-started { background: var(--ns-bg); color: var(--ns-fg); }
.pill.working     { background: var(--wk-bg); color: var(--wk-fg); }
.pill.complete    { background: var(--cp-bg); color: var(--cp-fg); }
.quick { margin-left: auto; display: flex; gap: 4px; }
.quick button {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 1px 8px; font-size: 11.5px; cursor: pointer;
}
.quick button:hover { border-color: var(--accent); }

.typing .bubble { color: var(--muted); font-style: italic; }

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 6px 12px; flex-shrink: 0; }
.chips:empty { display: none; }
.chip {
  border: 1px solid var(--border); background: var(--chip); border-radius: 999px;
  padding: 6px 12px; white-space: nowrap; cursor: pointer; font-size: 13.5px; color: var(--green);
}
@media (prefers-color-scheme: dark) { .chip { color: var(--accent); } }

.composer { display: flex; gap: 8px; padding: 8px 12px 12px; flex-shrink: 0; }
.composer input {
  flex: 1; border: 0; border-radius: 22px; padding: 11px 16px;
  background: var(--surface); box-shadow: var(--shadow); min-width: 0;
}
.send {
  width: 46px; height: 46px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: #fff; font-size: 18px; flex-shrink: 0;
}

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #111b21; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; z-index: 10;
}

/* ---------- tablet ---------- */
@media (max-width: 900px) {
  .chat-scroll { padding: 14px 3%; }
  .bubble { max-width: 85%; }
}

/* ---------- phones ---------- */
@media (max-width: 680px) {
  body { font-size: 15px; }
  /* 16px inputs stop iOS from zooming in on focus */
  input, select, .composer input { font-size: 16px; }

  .topbar { padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); gap: 8px; }
  .avatar { width: 34px; height: 34px; font-size: 17px; }
  .brand-name { font-size: 16px; }
  .switch-btn { padding: 7px 11px; font-size: 14px; }

  /* add form: Work full width, then Date | Sector, Status | button */
  #tableView { padding: 10px; gap: 10px; }
  .form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding: 12px; }
  .form label { min-width: 0; }
  .form label.grow { grid-column: 1 / -1; order: -1; }
  .form input, .form select { width: 100%; min-height: 44px; }
  .form-actions { align-self: end; }
  .form-actions .btn { flex: 1; min-height: 44px; padding: 9px 10px; }

  /* filters: search on its own line, two selects side by side */
  .filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .filters input { grid-column: 1 / -1; }
  .filters input, .filters select { width: 100%; min-height: 44px; min-width: 0; }

  /* table rows become cards */
  .table-wrap { background: transparent; box-shadow: none; overflow: visible; }
  thead { display: none; }
  table, tbody { display: block; width: 100%; }
  tbody { display: flex; flex-direction: column; gap: 8px; }
  tr {
    display: grid; grid-template-columns: 1fr 1fr auto; gap: 6px 8px; align-items: center;
    background: var(--surface); border-radius: 10px; box-shadow: var(--shadow); padding: 10px 10px 10px 12px;
  }
  tbody tr:hover { background: var(--surface); }
  td { display: block; border: 0; padding: 0; min-width: 0; }
  td.id { display: none; }
  td.work { grid-column: 1 / 3; grid-row: 1; font-weight: 600; font-size: 15.5px; word-break: break-word; }
  td.work::before { content: attr(data-num) " "; color: var(--muted); font-weight: 400; font-size: 13px; }
  td.date { grid-column: 3; grid-row: 1; justify-self: end; font-size: 12.5px; color: var(--muted); }
  td.sector { grid-column: 1; grid-row: 2; }
  td.status { grid-column: 2; grid-row: 2; }
  td.row-actions { grid-column: 3; grid-row: 2; }
  .cell-select { width: 100%; min-height: 38px; font-size: 14px; }
  .icon-btn { padding: 8px; font-size: 17px; }
  .empty { background: var(--surface); border-radius: 10px; box-shadow: var(--shadow); }

  /* chat */
  .chat-scroll { padding: 10px; }
  .bubble { max-width: 90%; font-size: 15px; }
  .titem-meta { gap: 4px 8px; }
  .quick { margin-left: 0; width: 100%; }
  .quick button { padding: 5px 12px; font-size: 13px; }
  .chips { padding: 6px 10px; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .chips::-webkit-scrollbar { display: none; }
  .composer { padding: 6px 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .send { width: 44px; height: 44px; }
  .toast { bottom: calc(80px + env(safe-area-inset-bottom)); max-width: calc(100% - 32px); }
}

/* very narrow phones */
@media (max-width: 360px) {
  .brand-sub { display: none; }
  .switch-btn span { display: none; }
}
