/* ==========================================================================
   社内基幹システム デザインシステム
   ========================================================================== */

:root {
    --color-bg: #f2f4fa;
    --color-surface: #ffffff;
    --color-border: #e5e8f1;
    --color-border-strong: #d3d9e6;
    --color-text: #1a2030;
    --color-text-muted: #656d80;
    --color-text-faint: #9aa2b1;

    --color-primary: #4353ff;
    --color-primary-hover: #3341e8;
    --color-primary-2: #7c6bff;
    --color-primary-soft: #eef0ff;

    --color-success: #16a34a;
    --color-success-soft: #e9f9ef;
    --color-warning: #d97706;
    --color-warning-soft: #fef6e6;
    --color-danger: #dc2626;
    --color-danger-soft: #fdeaea;
    --color-neutral-soft: #eef0f5;

    --sidebar-width: 256px;
    --header-height: 64px;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --shadow-xs: 0 1px 2px rgba(23, 27, 44, 0.05);
    --shadow-sm: 0 2px 6px rgba(23, 27, 44, 0.06);
    --shadow-md: 0 10px 28px rgba(23, 27, 44, 0.10);
    --shadow-lg: 0 18px 42px rgba(23, 27, 44, 0.14);
    --font-sans: "Segoe UI", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Noto Sans JP",
        -apple-system, BlinkMacSystemFont, sans-serif;
}

@media (prefers-reduced-motion: reduce) {
    * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
    margin: 0;
    font-family: var(--font-sans);
    background: var(--color-bg);
    background-image:
        radial-gradient(760px 420px at 100% -10%, rgba(67, 83, 255, 0.06), transparent 60%),
        radial-gradient(600px 360px at -5% 10%, rgba(124, 107, 255, 0.05), transparent 55%);
    background-attachment: fixed;
    color: var(--color-text);
    font-size: 15px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    letter-spacing: 0.01em;
}

h1, h2, h3, h4 {
    margin: 0 0 0.6em;
    font-weight: 700;
    line-height: 1.35;
    color: var(--color-text);
    letter-spacing: -0.01em;
}

h2 { font-size: 1.42rem; }
h3 { font-size: 1.08rem; margin-top: 1.6em; }

a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }
p { margin: 0 0 0.9em; }

::selection { background: var(--color-primary-soft); color: var(--color-primary-hover); }

:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
}

/* ------------------------------------------------------------------------
   全体レイアウト（サイドバー + ヘッダー + コンテンツ）
   ------------------------------------------------------------------------ */

.app-shell { display: flex; min-height: 100vh; }

/* サイドバー */
.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: linear-gradient(180deg, #14182a 0%, #10131f 100%);
    color: #cfd3e6;
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    overflow-y: auto;
    z-index: 30;
    transition: transform 0.2s ease;
    border-right: 1px solid rgba(255, 255, 255, 0.04);
}

.sidebar::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.1); border-radius: 999px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 1.3rem 1.3rem 1.15rem;
    font-weight: 700;
    font-size: 1.02rem;
    color: #ffffff;
    border-bottom: 1px solid rgba(255, 255, 255, 0.07);
    letter-spacing: -0.01em;
}

.sidebar-brand .brand-mark {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--color-primary), var(--color-primary-2));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.86rem;
    font-weight: 800;
    color: #fff;
    flex-shrink: 0;
    box-shadow: 0 6px 16px rgba(67, 83, 255, 0.45);
}

.sidebar-nav { flex: 1; padding: 1.1rem 0.8rem; }

.sidebar-group { margin-bottom: 1.5rem; }

.sidebar-group-title {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #6c7290;
    padding: 0 0.65rem;
    margin-bottom: 0.5rem;
    font-weight: 700;
}

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.58rem 0.7rem;
    border-radius: var(--radius-sm);
    color: #c2c7dc;
    font-size: 0.895rem;
    margin-bottom: 2px;
    position: relative;
    transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

.sidebar-link:hover {
    background: rgba(255, 255, 255, 0.06);
    color: #fff;
    text-decoration: none;
}
.sidebar-link:active { transform: scale(0.98); }

.sidebar-link.active {
    background: linear-gradient(90deg, rgba(67, 83, 255, 0.9), rgba(124, 107, 255, 0.75));
    color: #fff;
    font-weight: 600;
    box-shadow: 0 6px 18px rgba(67, 83, 255, 0.28);
}

.sidebar-link .nav-icon {
    width: 17px;
    height: 17px;
    flex-shrink: 0;
    opacity: 0.9;
}

.sidebar-footer {
    padding: 1rem 1.3rem 1.2rem;
    border-top: 1px solid rgba(255, 255, 255, 0.07);
    font-size: 0.76rem;
    color: #666c8a;
}

/* メインエリア */
.main-area { margin-left: var(--sidebar-width); flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
    height: var(--header-height);
    background: rgba(255, 255, 255, 0.82);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.8rem;
    position: sticky;
    top: 0;
    z-index: 20;
}

.topbar-title {
    font-weight: 700; font-size: 1.05rem; color: var(--color-text); letter-spacing: -0.01em;
    min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.topbar-user { display: flex; align-items: center; gap: 1rem; font-size: 0.87rem; color: var(--color-text-muted); flex-shrink: 0; }
.topbar-user .user-badge { display: flex; align-items: center; gap: 0.55rem; white-space: nowrap; }

.avatar-circle {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--color-primary), var(--color-primary-2));
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.82rem;
    box-shadow: 0 4px 10px rgba(67, 83, 255, 0.3);
}

.topbar-user .logout-link {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    color: var(--color-text-muted);
    padding: 0.4rem 0.7rem;
    border-radius: var(--radius-sm);
    transition: background 0.15s ease, color 0.15s ease;
    white-space: nowrap;
}
.topbar-user .logout-link svg { width: 15px; height: 15px; }
.topbar-user .logout-link:hover { background: var(--color-danger-soft); color: var(--color-danger); text-decoration: none; }

/* ------------------------------------------------------------------------
   通知ベル（トップバー）
   ------------------------------------------------------------------------ */

.notif-bell-wrap { position: relative; }

.notif-bell-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-sm);
    width: 2.2rem;
    height: 2.2rem;
    cursor: pointer;
    color: var(--color-text-muted);
    transition: background 0.15s ease, color 0.15s ease;
}
.notif-bell-btn:hover { background: var(--color-primary-soft); color: var(--color-primary); }
.notif-bell-btn svg { width: 18px; height: 18px; }

.notif-bell-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 999px;
    background: var(--color-danger);
    color: #fff;
    font-size: 0.66rem;
    font-weight: 700;
    line-height: 16px;
    text-align: center;
}

.notif-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 0.6rem);
    right: 0;
    width: 320px;
    max-width: 88vw;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    z-index: 40;
}

.notif-bell-wrap.open .notif-dropdown { display: block; }

@media (max-width: 640px) {
    /* 幅の狭い画面では、ベルボタン基準の right:0 だと左側の余白が足りず
       ドロップダウンが画面外にはみ出してしまう（実機テストで発覚）。
       トップバー基準の固定位置に切り替えて画面内に収める。 */
    .notif-dropdown {
        position: fixed;
        top: calc(var(--header-height) + 0.5rem);
        left: 1rem;
        right: 1rem;
        width: auto;
        max-width: none;
    }
}

.notif-dropdown-header {
    padding: 0.75rem 1rem;
    font-weight: 700;
    font-size: 0.85rem;
    border-bottom: 1px solid var(--color-border);
}

.notif-dropdown-list { max-height: 340px; overflow-y: auto; }

.notif-dropdown-item {
    display: flex;
    align-items: flex-start;
    gap: 0.55rem;
    padding: 0.7rem 1rem;
    border-bottom: 1px solid var(--color-border);
    color: var(--color-text);
}
.notif-dropdown-item:last-child { border-bottom: none; }
.notif-dropdown-item:hover { background: #f7f8fd; text-decoration: none; }

.notif-dropdown-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    margin-top: 0.4rem;
    flex-shrink: 0;
    background: transparent;
}
.notif-dropdown-item.unread .notif-dropdown-dot { background: var(--color-primary); }

.notif-dropdown-body { display: flex; flex-direction: column; gap: 0.15rem; min-width: 0; }
.notif-dropdown-subject { font-size: 0.85rem; color: var(--color-text); }
.notif-dropdown-item.unread .notif-dropdown-subject { font-weight: 700; }
.notif-dropdown-date { font-size: 0.74rem; color: var(--color-text-faint); }

.notif-dropdown-empty { padding: 1.4rem 1rem; text-align: center; color: var(--color-text-faint); font-size: 0.85rem; }

.notif-dropdown-footer {
    display: block;
    text-align: center;
    padding: 0.65rem 1rem;
    font-size: 0.82rem;
    font-weight: 600;
    border-top: 1px solid var(--color-border);
    background: #fafbfe;
}

.menu-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    background: none;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-sm);
    width: 2.2rem;
    height: 2.2rem;
    cursor: pointer;
}
.menu-toggle svg { width: 18px; height: 18px; }

.content { padding: 2rem 2.2rem 3.2rem; max-width: 1100px; width: 100%; animation: fade-up 0.25s ease; }

@keyframes fade-up {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ------------------------------------------------------------------------
   メッセージ / アラート
   ------------------------------------------------------------------------ */

.messages { list-style: none; margin: 0 0 1.3rem; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }

.alert {
    padding: 0.75rem 1.05rem;
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    font-size: 0.89rem;
    box-shadow: var(--shadow-xs);
}

.alert-success { background: var(--color-success-soft); color: #0f7a37; border-color: #bfe9cf; }
.alert-error, .alert-danger { background: var(--color-danger-soft); color: #b91c1c; border-color: #f6c4c4; }
.alert-warning { background: var(--color-warning-soft); color: #a15c04; border-color: #f3daa8; }
.alert-info, .alert-debug { background: var(--color-primary-soft); color: #3346c4; border-color: #cad0f8; }

/* ------------------------------------------------------------------------
   カード
   ------------------------------------------------------------------------ */

.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    padding: 1.6rem 1.7rem;
    margin-bottom: 1.5rem;
    transition: box-shadow 0.15s ease;
}

.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.15rem; gap: 1rem; flex-wrap: wrap; }
.card-header h2, .card-header h3 { margin: 0; }
.card-actions { display: flex; gap: 0.55rem; flex-wrap: wrap; }

.page-header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 0.9rem; margin-bottom: 1.5rem; }
.page-header .subtitle { color: var(--color-text-muted); font-size: 0.88rem; margin-top: 0.15rem; }

/* ダッシュボードのタイルグリッド */
.tile-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 1.1rem; margin-bottom: 1.7rem; }

.tile {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 1.4rem 1.4rem 1.2rem;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
    position: relative;
    overflow: hidden;
}

.tile::before {
    content: "";
    position: absolute;
    inset: 0 0 auto 0;
    height: 3px;
    background: linear-gradient(90deg, var(--color-primary), var(--color-primary-2));
    opacity: 0;
    transition: opacity 0.18s ease;
}

.tile:hover { transform: translateY(-3px); box-shadow: var(--shadow-lg); border-color: var(--color-border-strong); }
.tile:hover::before { opacity: 1; }

.tile-icon {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    background: var(--color-primary-soft);
    color: var(--color-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 0.25rem;
}
.tile-icon svg { width: 21px; height: 21px; }

.tile h3 { margin: 0; font-size: 1.02rem; }

.tile-links { display: flex; flex-direction: column; gap: 0.32rem; margin-top: 0.25rem; font-size: 0.88rem; }
.tile-links a { display: flex; align-items: center; gap: 0.4rem; color: var(--color-text-muted); }
.tile-links a::before {
    content: "";
    width: 4px; height: 4px; border-radius: 50%;
    background: var(--color-text-faint);
    flex-shrink: 0;
}
.tile-links a:hover { color: var(--color-primary); text-decoration: none; }
.tile-links a:hover::before { background: var(--color-primary); }

/* 統計チップ（残日数などの簡易サマリー） */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 1rem; margin-bottom: 1.7rem; }
.stat-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 1.25rem 1.4rem;
    box-shadow: var(--shadow-sm);
}
.stat-card .stat-label { font-size: 0.8rem; color: var(--color-text-muted); font-weight: 600; margin-bottom: 0.4rem; }
.stat-card .stat-value { font-size: 1.7rem; font-weight: 800; letter-spacing: -0.02em; color: var(--color-text); }
.stat-card .stat-value small { font-size: 0.95rem; font-weight: 600; color: var(--color-text-muted); }

.progress-bar { height: 8px; border-radius: 999px; background: var(--color-neutral-soft); overflow: hidden; margin-top: 0.7rem; }
.progress-bar > span {
    display: block;
    height: 100%;
    background: linear-gradient(90deg, var(--color-primary), var(--color-primary-2));
    border-radius: 999px;
}

/* ------------------------------------------------------------------------
   ボタン
   ------------------------------------------------------------------------ */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    padding: 0.6rem 1.15rem;
    border-radius: var(--radius-sm);
    font-size: 0.88rem;
    font-weight: 600;
    border: 1px solid transparent;
    cursor: pointer;
    background: var(--color-neutral-soft);
    color: var(--color-text);
    text-decoration: none;
    line-height: 1.2;
    transition: transform 0.08s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.btn:hover { text-decoration: none; }
.btn:active { transform: translateY(1px); }

.btn-primary { background: linear-gradient(135deg, var(--color-primary), var(--color-primary-hover)); color: #fff; box-shadow: 0 6px 16px rgba(67, 83, 255, 0.28); }
.btn-primary:hover { box-shadow: 0 8px 20px rgba(67, 83, 255, 0.36); color: #fff; }

.btn-outline { background: var(--color-surface); border-color: var(--color-border-strong); color: var(--color-text); }
.btn-outline:hover { border-color: var(--color-primary); color: var(--color-primary); }

.btn-danger { background: var(--color-danger-soft); color: var(--color-danger); }
.btn-danger:hover { background: #fbdada; color: var(--color-danger); }

.btn-sm { padding: 0.42rem 0.85rem; font-size: 0.82rem; }
.btn svg { width: 15px; height: 15px; }

/* ------------------------------------------------------------------------
   テーブル
   ------------------------------------------------------------------------ */

.table-wrap { overflow-x: auto; border: 1px solid var(--color-border); border-radius: var(--radius-md); }

table.table { width: 100%; border-collapse: collapse; background: var(--color-surface); font-size: 0.9rem; }

table.table th, table.table td { padding: 0.8rem 1rem; text-align: left; border-bottom: 1px solid var(--color-border); white-space: nowrap; }

table.table thead th, table.table tr:first-child th {
    background: #fafbfe;
    color: var(--color-text-muted);
    font-weight: 700;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    position: sticky;
    top: 0;
}

table.table tbody tr, table.table tr:not(:first-child) { transition: background 0.1s ease; }
table.table tbody tr:last-child td, table.table tr:last-child td { border-bottom: none; }
table.table tbody tr:hover, table.table tr:not(:first-child):hover { background: #f7f8fd; }

table.table td.empty-row { color: var(--color-text-faint); text-align: center; padding: 2rem; white-space: normal; }
table.table td.empty-row .empty-icon { display: block; margin: 0 auto 0.6rem; width: 30px; height: 30px; opacity: 0.5; }

/* 詳細表示用の縦型テーブル（項目名 / 値） */
table.table-detail { width: 100%; border-collapse: collapse; background: var(--color-surface); font-size: 0.92rem; }
table.table-detail th, table.table-detail td { padding: 0.75rem 1.05rem; border-bottom: 1px solid var(--color-border); text-align: left; vertical-align: top; }
table.table-detail th { width: 220px; background: #fafbfe; color: var(--color-text-muted); font-weight: 700; font-size: 0.84rem; }
table.table-detail tr:last-child th, table.table-detail tr:last-child td { border-bottom: none; }

.text-right { text-align: right; }

/* ------------------------------------------------------------------------
   バッジ（ステータス）
   ------------------------------------------------------------------------ */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.28rem 0.7rem;
    border-radius: 999px;
    font-size: 0.77rem;
    font-weight: 700;
    background: var(--color-neutral-soft);
    color: var(--color-text-muted);
    white-space: nowrap;
    letter-spacing: 0.01em;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: 0.85; }

.badge-pending, .badge-unsettled { background: var(--color-warning-soft); color: #a15c04; }
.badge-approved, .badge-settled, .badge-confirmed { background: var(--color-success-soft); color: #0f7a37; }
.badge-remanded, .badge-rejected, .badge-error { background: var(--color-danger-soft); color: #b91c1c; }
.badge-withdrawn, .badge-unsubmitted { background: var(--color-neutral-soft); color: var(--color-text-muted); }
.badge-extracted { background: var(--color-primary-soft); color: var(--color-primary); }

/* ------------------------------------------------------------------------
   フォーム（Djangoの as_p 出力をそのままスタイリング）
   ------------------------------------------------------------------------ */

form p { margin: 0 0 1.15rem; display: flex; flex-direction: column; gap: 0.38rem; }
form label { font-weight: 600; font-size: 0.86rem; color: var(--color-text); }

form input[type="text"],
form input[type="number"],
form input[type="date"],
form input[type="email"],
form input[type="password"],
form input[type="file"],
form select,
form textarea {
    width: 100%;
    max-width: 480px;
    padding: 0.62rem 0.85rem;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-sm);
    font-size: 0.92rem;
    font-family: inherit;
    background: var(--color-surface);
    color: var(--color-text);
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

form input:focus, form select:focus, form textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 4px var(--color-primary-soft);
}

form textarea { min-height: 90px; resize: vertical; }
form .helptext { font-size: 0.78rem; color: var(--color-text-faint); font-weight: 400; }

form ul.errorlist {
    list-style: none;
    margin: 0.2rem 0 0;
    padding: 0.55rem 0.8rem;
    background: var(--color-danger-soft);
    color: var(--color-danger);
    border-radius: var(--radius-sm);
    font-size: 0.82rem;
}

.filter-bar { display: flex; align-items: flex-end; gap: 1.1rem; flex-wrap: wrap; margin-bottom: 0.4rem; }
.filter-bar label { display: flex; flex-direction: column; gap: 0.32rem; font-weight: 600; font-size: 0.84rem; color: var(--color-text); }
.filter-bar input, .filter-bar select {
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.filter-bar input:focus, .filter-bar select:focus { outline: none; border-color: var(--color-primary); box-shadow: 0 0 0 4px var(--color-primary-soft); }
.filter-bar .btn { height: 2.5rem; }

/* ユーティリティ */
.text-muted { color: var(--color-text-muted); }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }
.section-actions { display: flex; gap: 0.6rem; flex-wrap: wrap; margin-bottom: 1.2rem; }
.empty-state { text-align: center; padding: 2.6rem 1rem; color: var(--color-text-faint); }
.empty-state svg { width: 32px; height: 32px; margin: 0 auto 0.7rem; opacity: 0.5; display: block; }

/* ------------------------------------------------------------------------
   レスポンシブ
   ------------------------------------------------------------------------ */

@media (max-width: 900px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); box-shadow: var(--shadow-lg); }
    .main-area { margin-left: 0; }
    .menu-toggle { display: inline-flex; }
    .content { padding: 1.2rem 1.1rem 2.4rem; }
    table.table-detail th { width: 140px; }
}
