/* =========================================================
   mobile.css — Selvio mobile overrides
   ALL rules are inside @media (max-width: 640px).
   Zero desktop styles are changed here.
   ========================================================= */

@media (max-width: 640px) {

  /* ── LAYOUT ─────────────────────────────────────────── */
  html, body {
    height: 100dvh;
    overflow: hidden;
  }

  /* Hide desktop sidebar */
  .sidebar {
    display: none !important;
  }

  /* Main takes full width */
  .main {
    height: 100dvh;
    overflow: hidden;
  }

  /* Topbar compact */
  .topbar {
    padding: 10px 16px;
  }
  .topbar-date {
    display: none;
  }

  /* Content scrolls, leaves room for tab bar */
  .content {
    flex-direction: column;
    padding: 12px 14px 80px;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    gap: 14px;
  }

  /* Dashboard columns: flex:none so they take natural height
     and .content (not the columns themselves) can scroll */
  .left-col {
    flex: none !important;
    width: 100% !important;
    overflow-y: visible !important;
    overflow-x: visible !important;
    min-height: unset !important;
  }
  .right-col {
    flex: none !important;
    width: 100% !important;
    overflow-y: visible !important;
    min-height: unset !important;
    flex-shrink: unset !important;
  }

  /* Subpage also clears tab bar and scrolls */
  #subpage {
    padding-bottom: 80px !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    /* remove padding-left/right: subpages set their own */
  }

  /* ── DEMO BANNER ─────────────────────────────────────── */
  #demoBanner {
    padding: 10px 14px !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  #demoBanner > div {
    gap: 6px !important;
  }
  /* Make ✕ bigger so it's easier to tap */
  #m-demo-close {
    min-width: 36px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
  }

  /* ── METRICS GRID ────────────────────────────────────── */
  .metrics-row {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px;
  }
  .m-value {
    font-size: 18px !important;
  }

  /* ── TRANSACTIONS — hide table, show cards ───────────── */
  /* Table inside transactions section */
  #txList table,
  #subpage table {
    display: none !important;
  }

  /* Mobile card list containers (injected by mobile.js) */
  .m-card-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .m-tx-card {
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 14px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    transition: background 0.1s;
  }
  .m-tx-card:active { background: var(--bg3); }

  .m-tx-icon {
    width: 36px;
    height: 36px;
    border-radius: 9px;
    background: var(--bg3);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    flex-shrink: 0;
  }
  .m-tx-info { flex: 1; min-width: 0; }
  .m-tx-name {
    font-size: 13px;
    font-weight: 500;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .m-tx-meta {
    font-size: 11px;
    color: var(--text3);
    margin-top: 2px;
    font-family: var(--font-mono);
  }
  .m-tx-right { text-align: right; flex-shrink: 0; }
  .m-tx-amount {
    font-size: 13px;
    font-weight: 600;
    font-family: var(--font-mono);
  }
  .m-tx-amount.in  { color: var(--accent); }
  .m-tx-amount.out { color: var(--text2); }
  .m-tx-badge {
    font-size: 10px;
    padding: 2px 7px;
    border-radius: 10px;
    font-family: var(--font-mono);
    display: inline-block;
    margin-top: 3px;
  }
  .m-tx-badge.paid    { background: var(--accent-dim); color: var(--accent); }
  .m-tx-badge.pending { background: var(--warn-dim);   color: var(--warn); }
  .m-tx-badge.overdue,
  .m-tx-badge.risk    { background: var(--danger-dim); color: var(--danger); }

  /* ── JOURNAL CARDS ───────────────────────────────────── */
  .m-journal-card {
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
    margin-bottom: 8px;
  }
  .m-journal-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    cursor: pointer;
    user-select: none;
  }
  .m-journal-header:active { background: var(--bg3); }
  .m-journal-voucher {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--text2);
    flex-shrink: 0;
  }
  .m-journal-desc {
    flex: 1;
    font-size: 13px;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .m-journal-date {
    font-size: 11px;
    color: var(--text3);
    font-family: var(--font-mono);
    flex-shrink: 0;
  }
  .m-journal-chevron {
    font-size: 11px;
    color: var(--text3);
    transition: transform 0.2s;
    flex-shrink: 0;
  }
  .m-journal-card.open .m-journal-chevron {
    transform: rotate(180deg);
  }
  .m-journal-lines {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.25s ease;
    border-top: 0px solid var(--border);
  }
  .m-journal-card.open .m-journal-lines {
    max-height: 600px;
    border-top-width: 1px;
  }
  .m-journal-line {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 14px;
    border-bottom: 1px solid var(--border);
    font-size: 12px;
    font-family: var(--font-mono);
  }
  .m-journal-line:last-child { border-bottom: none; }
  .m-journal-line-acct { color: var(--text2); flex: 1; }
  .m-journal-line-debit  { color: var(--accent); min-width: 60px; text-align: right; }
  .m-journal-line-credit { color: var(--blue);   min-width: 60px; text-align: right; }

  /* ── ACCOUNT CARDS ───────────────────────────────────── */
  .m-account-card {
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 14px;
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
  }
  .m-account-num {
    font-family: var(--font-mono);
    font-size: 14px;
    font-weight: 500;
    color: var(--text2);
    min-width: 42px;
  }
  .m-account-info { flex: 1; min-width: 0; }
  .m-account-name {
    font-size: 13px;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .m-account-type {
    font-size: 10px;
    margin-top: 3px;
    display: inline-block;
    padding: 1px 7px;
    border-radius: 8px;
    font-family: var(--font-mono);
    background: var(--bg3);
    color: var(--text3);
  }
  .m-account-balance {
    font-family: var(--font-mono);
    font-size: 13px;
    font-weight: 600;
    text-align: right;
  }

  /* ── CHAT — hide desktop panel, FAB shows ────────────── */
  .chat-panel {
    display: none !important;
  }

  /* ── AI FAB ──────────────────────────────────────────── */
  #m-ai-fab {
    position: fixed;
    bottom: 76px;
    right: 16px;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--accent);
    color: #0e0f11;
    border: none;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 200;
    box-shadow: 0 4px 20px rgba(0,212,170,0.4);
    transition: transform 0.15s;
  }
  #m-ai-fab:active { transform: scale(0.92); }

  /* ── BOTTOM TAB BAR ──────────────────────────────────── */
  #m-tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 64px;
    background: var(--bg2);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: stretch;
    z-index: 300;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .m-tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    cursor: pointer;
    color: var(--text3);
    font-size: 10px;
    font-family: var(--font-mono);
    border: none;
    background: none;
    padding: 0;
    transition: color 0.15s;
    -webkit-tap-highlight-color: transparent;
  }
  .m-tab.active { color: var(--accent); }
  .m-tab-icon { font-size: 17px; line-height: 1; }
  .m-tab-label { font-size: 10px; line-height: 1; }

  /* ── KIRJAT BOTTOM SHEET ─────────────────────────────── */
  #m-kirjat-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg2);
    border-top: 1px solid var(--border);
    border-radius: 20px 20px 0 0;
    z-index: 400;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  #m-kirjat-sheet.open {
    transform: translateY(0);
  }
  .m-sheet-handle {
    width: 36px;
    height: 4px;
    background: var(--border2);
    border-radius: 2px;
    margin: 10px auto 8px;
  }
  .m-sheet-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
    padding: 4px 20px 12px;
    border-bottom: 1px solid var(--border);
  }
  .m-sheet-items {
    display: flex;
    flex-direction: column;
    padding: 8px 10px 16px;
  }
  .m-sheet-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 10px;
    border-radius: 10px;
    cursor: pointer;
    color: var(--text2);
    font-size: 14px;
    -webkit-tap-highlight-color: transparent;
  }
  .m-sheet-item:active { background: var(--bg3); }
  .m-sheet-item.active { color: var(--accent); }
  .m-sheet-icon { font-size: 16px; width: 22px; text-align: center; }

  /* ── SHEET BACKDROP ──────────────────────────────────── */
  #m-sheet-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 350;
    display: none;
    -webkit-tap-highlight-color: transparent;
  }
  #m-sheet-backdrop.visible { display: block; }

  /* ── AI CHAT BOTTOM SHEET ────────────────────────────── */
  #m-ai-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 80dvh;
    background: var(--bg2);
    border-radius: 20px 20px 0 0;
    border-top: 1px solid var(--border);
    z-index: 500;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    display: flex;
    flex-direction: column;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  #m-ai-sheet.open {
    transform: translateY(0);
  }
  #m-ai-sheet-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 450;
    display: none;
  }
  #m-ai-sheet-backdrop.visible { display: block; }

  #m-ai-sheet-header {
    padding: 10px 16px 12px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
  }
  #m-ai-sheet-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text);
  }
  #m-ai-sheet-close {
    background: none;
    border: none;
    color: var(--text3);
    font-size: 20px;
    cursor: pointer;
    padding: 4px;
    line-height: 1;
  }
  #m-ai-sheet-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
  }
  #m-ai-sheet-input-row {
    display: flex;
    gap: 8px;
    padding: 10px 14px 14px;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
  }
  #m-ai-sheet-input {
    flex: 1;
    background: var(--bg3);
    border: 1px solid var(--border2);
    border-radius: 10px;
    padding: 9px 12px;
    font-size: 16px; /* prevents iOS zoom */
    color: var(--text);
    font-family: var(--font-body);
    outline: none;
    resize: none;
    min-height: 40px;
    max-height: 100px;
  }
  #m-ai-sheet-send {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: var(--accent);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }
  #m-ai-sheet-send svg { width: 16px; height: 16px; fill: #0e0f11; }
  #m-ai-sheet-send:disabled { opacity: 0.5; }

  /* ── MODAL → BOTTOM SHEET ────────────────────────────── */
  /* When JS adds class m-bottom-sheet to a modal overlay */
  .m-bottom-sheet {
    align-items: flex-end !important;
    padding: 0 !important;
  }
  .m-bottom-sheet > div:first-child {
    border-radius: 20px 20px 0 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    max-height: 90dvh;
    overflow-y: auto;
    padding-bottom: env(safe-area-inset-bottom, 16px) !important;
    animation: slideUpSheet 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  }
  @keyframes slideUpSheet {
    from { transform: translateY(60px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
  }

  /* ── INPUTS — prevent iOS zoom ───────────────────────── */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="date"],
  input[type="search"],
  textarea,
  select {
    font-size: 16px !important;
  }

  /* ── MISC ────────────────────────────────────────────── */
  /* Hide horizontal overflow from tables that remain visible */
  .chart-wrap { height: 160px !important; }

  /* Stat cards horizontal scroll */
  #jStats,
  #acStats,
  [id$="Stats"] {
    flex-wrap: nowrap !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }

  /* ── ACTION CARDS (Suunnitelma) ──────────────────────── */
  /* Prevent badge + saving from overflowing the card header.
     JS (mobile.js) hides the saving badge; here we make the
     urgency badge truncate instead of overflowing. */
  [id^="acard-"] > div:first-child > span[style*="border-radius:20px"] {
    max-width: 110px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* Why text can wrap on mobile */
  [id^="acard-"] div[style*="text-overflow:ellipsis"] {
    white-space: normal !important;
    overflow: visible !important;
    text-overflow: unset !important;
  }

  /* ── KASSAENNUSTE GRID (Suunnitelma) ─────────────────── */
  /* The forecast table uses inline grid — make it scrollable */
  .m-forecast-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -14px;
    padding: 0 14px;
  }
  .m-forecast-scroll > * {
    min-width: 340px;
  }

  /* ── SWIPE HANDLE visual feedback ───────────────────── */
  .m-sheet-handle,
  #m-ai-sheet > .m-sheet-handle {
    cursor: grab;
  }
  .m-sheet-handle:active,
  #m-ai-sheet > .m-sheet-handle:active {
    cursor: grabbing;
    background: var(--text3);
  }
}
