:root {
  --bg: var(--tg-theme-bg-color, #f4f6f8);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #17212b);
  --muted: var(--tg-theme-hint-color, #6d7885);
  --link: var(--tg-theme-link-color, #1677ff);
  --button: var(--tg-theme-button-color, #1677ff);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --border: color-mix(in srgb, var(--muted) 25%, transparent);
  --danger: #c5353f;
  --success: #198754;
  --warning: #a06100;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-width: 280px;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

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

.app-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  padding: calc(16px + env(safe-area-inset-top, 0px)) 18px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 14px;
}

.back-to-tasks {
  display: none;
  color: var(--link);
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
}

.back-to-tasks:hover,
.back-to-tasks:focus-visible { text-decoration: underline; outline: none; }

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 21px; line-height: 1.15; letter-spacing: -0.02em; }
h2 { margin-bottom: 5px; font-size: 20px; letter-spacing: -0.015em; }
h3 { margin-bottom: 0; font-size: 15px; }

.eyebrow {
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.connection { color: var(--muted); font-size: 12px; white-space: nowrap; }
.connection.ok { color: var(--success); }
.connection.error { color: var(--danger); }

.app-shell { width: min(100%, 760px); margin: 0 auto; }
.view { padding-bottom: calc(94px + var(--safe-bottom)); }

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  margin: 12px 14px;
  padding: 3px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--muted) 12%, transparent);
}

.segment {
  min-height: 38px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-weight: 650;
}

.segment.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 4px rgb(0 0 0 / 7%); }
.segment span { margin-left: 3px; }

.form-intro, .list-heading { padding: 15px 18px 10px; }
.form-intro p, .list-heading p { margin-bottom: 0; color: var(--muted); font-size: 14px; line-height: 1.45; }

.form-section {
  position: relative;
  margin: 0;
  padding: 18px;
  border: 0;
  background: var(--surface);
  box-shadow: inset 0 1px var(--border);
}

.form-section + .form-section { margin-top: 10px; }
.form-section legend { float: left; width: 100%; margin: 0 0 8px; padding: 0; font-size: 16px; font-weight: 750; }
.form-section > legend + label,
.form-section > legend + .file-picker,
.form-section > .lines { clear: both; }

.request-kind-options {
  display: grid;
  gap: 9px;
  clear: both;
}

.request-kind-option {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 0;
  padding: 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
}

.request-kind-option:has(input:checked) {
  border-color: var(--link);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--link) 14%, transparent);
}

.request-kind-option:focus-within { border-color: var(--link); }
.request-kind-option input {
  flex: 0 0 auto;
  width: 18px;
  min-height: 18px;
  margin: 2px 0 0;
  padding: 0;
  accent-color: var(--link);
  box-shadow: none;
}
.request-kind-option strong,
.request-kind-option small { display: block; }
.request-kind-option small { margin-top: 3px; color: var(--muted); font-size: 12px; font-weight: 400; line-height: 1.35; }

label {
  display: block;
  margin: 13px 0 6px;
  color: var(--text);
  font-size: 13px;
  font-weight: 650;
}

input, select, textarea {
  display: block;
  width: 100%;
  min-height: 45px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  outline: none;
  background: var(--bg);
  color: var(--text);
}

textarea { min-height: 70px; resize: vertical; line-height: 1.4; }
input:focus, select:focus, textarea:focus { border-color: var(--link); box-shadow: 0 0 0 3px color-mix(in srgb, var(--link) 18%, transparent); }
input:user-invalid, select:user-invalid, textarea:user-invalid { border-color: var(--danger); }

.field-help {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.resolved-reference {
  margin: 13px 0 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: color-mix(in srgb, var(--link) 5%, var(--surface));
  color: var(--text);
  font-size: 13px;
  line-height: 1.4;
}

.line-title, .list-heading, .dialog-header, .total-row, .submit-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.text-button { border: 0; padding: 5px 0; background: transparent; color: var(--link); font-size: 13px; font-weight: 700; }
.section-action { position: absolute; top: 13px; right: 18px; }

.payment-line { padding: 5px 0 18px; }
.payment-line + .payment-line { margin-top: 12px; padding-top: 18px; border-top: 1px dashed var(--border); }
.remove-line { border: 0; background: transparent; color: var(--danger); font-size: 13px; }

.total-row { margin-top: 10px; padding-top: 15px; border-top: 1px solid var(--border); }
.total-row strong { font-size: 19px; }

.optional-fields { margin-top: 16px; }
.optional-fields summary { color: var(--link); cursor: pointer; font-size: 14px; font-weight: 650; }
.optional-mark { color: var(--muted); font-size: 12px; font-weight: 500; }

#attachments { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; }
.file-picker {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 12px 0 0;
  padding: 13px;
  border: 1px dashed color-mix(in srgb, var(--link) 55%, var(--border));
  border-radius: 11px;
  background: color-mix(in srgb, var(--link) 6%, var(--surface));
  color: var(--link);
  cursor: pointer;
}
.file-picker-icon { font-size: 25px; font-weight: 300; }
.file-picker strong, .file-picker small { display: block; }
.file-picker small { margin-top: 2px; color: var(--muted); font-weight: 400; }
.file-list { margin: 9px 0 0; padding: 0; list-style: none; }
.file-list li { padding: 7px 0; color: var(--muted); font-size: 13px; }

.submit-bar {
  position: fixed;
  z-index: 10;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 11px max(14px, calc((100vw - 730px) / 2)) calc(11px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(14px);
}
.submit-bar span, .submit-bar strong { display: block; }
.submit-bar span { color: var(--muted); font-size: 11px; }
.submit-bar strong { margin-top: 2px; font-size: 17px; }

.button {
  min-height: 43px;
  padding: 10px 17px;
  border: 0;
  border-radius: 10px;
  font-weight: 750;
}
.button.primary { background: var(--button); color: var(--button-text); }
.button.secondary { background: color-mix(in srgb, var(--muted) 14%, transparent); color: var(--text); }
.button.approve { background: var(--success); color: #fff; }
.button.block { background: var(--danger); color: #fff; }
.button:disabled { cursor: wait; opacity: .58; }

.inline-error { margin: 10px 14px; padding: 11px 13px; border-left: 3px solid var(--danger); background: color-mix(in srgb, var(--danger) 9%, var(--surface)); color: var(--danger); font-size: 13px; }

.state-panel, .empty-list { padding: 56px 24px; text-align: center; }
.state-panel p, .empty-list p { color: var(--muted); line-height: 1.5; }
.state-icon { display: grid; width: 44px; height: 44px; margin: 0 auto 14px; place-items: center; border: 2px solid var(--danger); border-radius: 50%; color: var(--danger); font-size: 23px; font-weight: 800; }

.icon-button { width: 40px; height: 40px; padding: 0; border: 0; border-radius: 50%; background: color-mix(in srgb, var(--muted) 12%, transparent); color: var(--text); font-size: 21px; }
.request-list { margin: 0; padding: 0 14px; list-style: none; }
.request-card { margin-bottom: 9px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.request-card button { width: 100%; padding: 15px; border: 0; background: transparent; color: inherit; text-align: left; }
.request-card-top { display: flex; justify-content: space-between; gap: 10px; }
.request-card-title { font-weight: 750; }
.request-card-meta { margin-top: 7px; color: var(--muted); font-size: 12px; }
.status { display: inline-block; padding: 3px 7px; border-radius: 6px; background: color-mix(in srgb, var(--muted) 12%, transparent); color: var(--muted); font-size: 11px; font-weight: 750; }
.status.approved { background: color-mix(in srgb, var(--success) 12%, transparent); color: var(--success); }
.status.blocked { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.status.needs_clarification { background: color-mix(in srgb, var(--warning) 12%, transparent); color: var(--warning); }

.request-dialog { width: min(100% - 20px, 620px); max-height: calc(100vh - 20px); padding: 0; border: 0; border-radius: 16px; background: var(--surface); color: var(--text); }
.request-dialog::backdrop { background: rgb(0 0 0 / 45%); }
.dialog-header { position: sticky; top: 0; z-index: 2; padding: 15px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.dialog-header h2 { margin: 0; }
#dialog-content { padding: 15px 16px; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.detail-item span { display: block; color: var(--muted); font-size: 11px; }
.detail-item strong { display: block; margin-top: 3px; font-size: 14px; }
.detail-wide { grid-column: 1 / -1; }
.attachment-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 8px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.attachment-item:last-child { border-bottom: 0; }
.attachment-info { min-width: 0; }
.attachment-info strong { overflow: hidden; margin: 0; text-overflow: ellipsis; white-space: nowrap; }
.attachment-info small { display: block; margin-top: 3px; color: var(--muted); }
.attachment-actions { display: flex; flex: 0 0 auto; gap: 6px; }
.attachment-action { padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: transparent; color: var(--link); font: inherit; font-size: 12px; font-weight: 700; }
.attachment-action.primary { border-color: color-mix(in srgb, var(--link) 45%, var(--border)); background: color-mix(in srgb, var(--link) 8%, transparent); }
.attachment-action:disabled { opacity: .55; }
.event-list { margin: 16px 0 0; padding: 14px 0 0; border-top: 1px solid var(--border); list-style: none; }
.event-list li { margin: 0 0 10px; padding-left: 12px; border-left: 2px solid var(--border); font-size: 13px; }
.event-list time { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
.comment-panel { padding: 12px 16px; border-top: 1px solid var(--border); background: var(--surface); }
.comment-panel label { margin-top: 0; }
.comment-panel .button { width: 100%; margin-top: 9px; }
.decision-panel { position: sticky; bottom: 0; padding: 12px 16px calc(12px + var(--safe-bottom)); border-top: 1px solid var(--border); background: var(--surface); }
.decision-panel label { margin-top: 0; }
.decision-note { margin: 0 0 10px; padding: 10px 12px; border-left: 3px solid var(--warning); background: color-mix(in srgb, var(--warning) 10%, var(--surface)); color: var(--text); font-size: 13px; line-height: 1.4; }
.decision-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; margin-top: 9px; }
.decision-actions .block { grid-column: 1 / -1; }

.attachment-preview-dialog { width: min(100% - 20px, 900px); height: min(100% - 20px, 820px); padding: 0; overflow: hidden; border: 0; border-radius: 16px; background: var(--surface); color: var(--text); }
.attachment-preview-dialog[open] { display: flex; flex-direction: column; }
.attachment-preview-dialog::backdrop { background: rgb(0 0 0 / 62%); }
.attachment-preview-header { display: flex; align-items: center; justify-content: space-between; }
.attachment-preview-title { overflow: hidden; max-width: calc(100vw - 100px); text-overflow: ellipsis; white-space: nowrap; }
.attachment-preview-context { display: flex; flex: 0 0 auto; align-items: center; gap: 8px; padding: 9px 14px; border-bottom: 1px solid var(--border); background: color-mix(in srgb, var(--link) 6%, var(--surface)); color: var(--muted); font-size: 12px; }
.attachment-preview-kind { flex: 0 0 auto; padding: 4px 7px; border: 1px solid color-mix(in srgb, var(--link) 38%, var(--border)); border-radius: 999px; background: color-mix(in srgb, var(--link) 10%, var(--surface)); color: var(--link); font-weight: 750; }
#attachment-preview-status[data-state="error"] { color: var(--danger); }
.attachment-preview-body { display: grid; flex: 1 1 auto; min-height: 0; overflow: auto; padding: 14px; place-items: center; background-color: color-mix(in srgb, var(--bg) 76%, #000 4%); background-image: linear-gradient(45deg, rgb(255 255 255 / 3%) 25%, transparent 25%), linear-gradient(-45deg, rgb(255 255 255 / 3%) 25%, transparent 25%), linear-gradient(45deg, transparent 75%, rgb(255 255 255 / 3%) 75%), linear-gradient(-45deg, transparent 75%, rgb(255 255 255 / 3%) 75%); background-position: 0 0, 0 8px, 8px -8px, -8px 0; background-size: 16px 16px; }
.attachment-preview-body > * { grid-area: 1 / 1; }
.attachment-preview-body [hidden] { display: none !important; }
.attachment-preview-loading { z-index: 1; max-width: 290px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--muted); font-size: 13px; text-align: center; box-shadow: 0 8px 24px rgb(0 0 0 / 14%); }
.attachment-preview-body img { display: block; max-width: 100%; max-height: 100%; border: 2px solid color-mix(in srgb, var(--link) 55%, var(--border)); border-radius: 10px; background: #000; object-fit: contain; box-shadow: 0 12px 34px rgb(0 0 0 / 34%); }
.attachment-preview-body iframe { width: 100%; height: 100%; border: 2px solid color-mix(in srgb, var(--link) 55%, var(--border)); border-radius: 10px; background: #fff; box-shadow: 0 12px 34px rgb(0 0 0 / 24%); }

.toast { position: fixed; z-index: 30; right: 14px; bottom: calc(82px + var(--safe-bottom)); left: 14px; max-width: 500px; margin: 0 auto; padding: 12px 14px; border-radius: 10px; background: #17212b; color: #fff; box-shadow: 0 8px 30px rgb(0 0 0 / 25%); font-size: 13px; text-align: center; }

@media (min-width: 600px) {
  .app-header { padding-right: max(24px, calc((100vw - 720px) / 2)); padding-left: max(24px, calc((100vw - 720px) / 2)); }
  .form-section { border-radius: var(--radius); margin: 0 14px; box-shadow: inset 0 0 0 1px var(--border); }
  .form-section + .form-section { margin-top: 10px; }
  .form-intro { padding-top: 22px; }
}

@media (min-width: 900px) {
  body.desktop-mode {
    --bg: #eef2f5;
    --surface: #ffffff;
    --text: #17212b;
    --muted: #66727f;
    --link: #0f766e;
    --button: #0f766e;
    --button-text: #ffffff;
  }

  body.desktop-mode .app-header {
    align-items: center;
    padding: 20px max(32px, calc((100vw - 1180px) / 2));
  }

  body.desktop-mode .back-to-tasks { display: inline-flex; }
  body.desktop-mode .app-shell { width: min(calc(100% - 48px), 1180px); }
  body.desktop-mode .view { padding-bottom: 34px; }
  body.desktop-mode .segmented {
    width: min(100%, 430px);
    margin: 18px 0;
  }

  body.desktop-mode #money-form {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 16px;
  }

  body.desktop-mode .form-intro,
  body.desktop-mode .request-kind-section,
  body.desktop-mode .payment-lines-section,
  body.desktop-mode .inline-error,
  body.desktop-mode .submit-bar { grid-column: 1 / -1; }

  body.desktop-mode .form-intro { padding: 8px 2px 2px; }
  body.desktop-mode .request-kind-options { grid-template-columns: 1fr 1fr; }
  body.desktop-mode .form-section {
    margin: 0;
    border-radius: var(--radius);
    box-shadow: inset 0 0 0 1px var(--border), 0 4px 18px rgb(23 33 43 / 4%);
  }
  body.desktop-mode .form-section + .form-section { margin-top: 0; }

  body.desktop-mode .payment-line {
    display: grid;
    grid-template-columns: minmax(170px, .32fr) minmax(0, 1fr);
    column-gap: 18px;
    align-items: center;
  }
  body.desktop-mode .payment-line .line-title,
  body.desktop-mode .payment-line .amount-row { grid-column: 1 / -1; }
  body.desktop-mode .payment-line > label { margin: 10px 0 6px; }
  body.desktop-mode .payment-line .amount-row > div {
    display: grid;
    grid-template-columns: minmax(170px, .32fr) minmax(0, 1fr);
    column-gap: 18px;
    align-items: center;
  }
  body.desktop-mode .payment-line .amount-row label { margin: 10px 0 6px; }
  body.desktop-mode .payment-line + .payment-line { grid-column: 1 / -1; }
  body.desktop-mode .total-row { grid-column: 1 / -1; }

  body.desktop-mode .submit-bar {
    position: static;
    right: auto;
    bottom: auto;
    left: auto;
    margin-top: 2px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 12px 30px rgb(23 33 43 / 12%);
  }

  body.desktop-mode .request-dialog { width: min(100% - 40px, 760px); }
}

@media (max-width: 899px) {
  .attachment-item { align-items: flex-start; flex-direction: column; }
  .attachment-actions { width: 100%; }
  .attachment-action { flex: 1; }
  body.desktop-mode .header-actions {
    align-items: flex-end;
    flex-direction: column-reverse;
    gap: 4px;
  }
  body.desktop-mode .back-to-tasks { display: inline-flex; }
}

@media (prefers-reduced-motion: no-preference) {
  .toast { animation: toast-in .18s ease-out; }
  @keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }
}
