<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Akkurtcast - Takvim</title>
    <link rel="icon" type="image/png" href="/static/img/icon.png">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
    <style>
        :root {
            --sidebar-bg: #1a1d21;
            --sidebar-hover: #2a2e35;
            --sidebar-active: #0d6efd;
            --sidebar-text: #a8b2c0;
            --topbar-bg: #fff;
            --page-bg: #f0f2f5;
            --card-bg: #fff;
            --card-border: #e2e8f0;
            --accent: #0d6efd;
        }
        * { box-sizing: border-box; }
        body { background: var(--page-bg); margin: 0; font-family: system-ui, -apple-system, sans-serif; }
        .sidebar {
            min-height: 100vh; background: var(--sidebar-bg);
            display: flex; flex-direction: column;
            position: sticky; top: 0; height: 100vh; overflow-y: auto;
        }
        .brand {
            display: flex; align-items: center; gap: 10px;
            padding: 20px 18px 16px; color: #fff;
            font-weight: 800; font-size: 1.05rem; letter-spacing: -0.3px;
            border-bottom: 1px solid #2a2e35; flex-shrink: 0;
        }
        .brand img { width: 46px; height: 46px; object-fit: contain; border-radius: 8px; }
        .brand-mark { display: flex; flex-direction: column; min-width: 0; line-height: 1.05; }
        .brand-name { color: #f8fafc; font-size: 1.18rem; font-weight: 850; letter-spacing: .2px; text-shadow: 0 1px 0 rgba(0,0,0,.25); }
        .brand-subtitle { margin-top: 4px; color: #7dd3fc; font-size: .63rem; font-weight: 700; letter-spacing: 1.25px; text-transform: uppercase; }
        .nav-section-label {
            font-size: 10px; font-weight: 700; text-transform: uppercase;
            letter-spacing: 1px; color: #4a5568; padding: 14px 18px 4px;
        }
        .sidebar a {
            color: var(--sidebar-text); display: flex; align-items: center; gap: 8px;
            padding: 9px 18px; text-decoration: none; font-size: 0.875rem;
            font-weight: 500; border-left: 3px solid transparent; transition: all 0.15s;
        }
        .sidebar a:hover { background: var(--sidebar-hover); color: #fff; }
        .sidebar a.active { background: rgba(13,110,253,0.12); color: #4d9fff; border-left-color: var(--accent); }
        .sidebar a .nav-icon { width: 22px; height: 22px; min-width: 22px; border-radius: 7px; display: inline-flex; align-items: center; justify-content: center; color: #fff; font-size: .78rem; line-height: 1; box-shadow: inset 0 1px 0 rgba(255,255,255,.18), 0 6px 14px rgba(0,0,0,.18); }
.nav-icon-panel { background: linear-gradient(135deg,#2563eb,#38bdf8); }
.nav-icon-music { background: linear-gradient(135deg,#7c3aed,#ec4899); }
.nav-icon-jingle { background: linear-gradient(135deg,#f59e0b,#f97316); }
.nav-icon-library { background: linear-gradient(135deg,#0f766e,#22c55e); }
.nav-icon-calendar { background: linear-gradient(135deg,#dc2626,#fb7185); }
.nav-icon-automation { background: linear-gradient(135deg,#475569,#94a3b8); }
.nav-icon-news { background: linear-gradient(135deg,#0891b2,#06b6d4); }
.nav-icon-broadcast { background: linear-gradient(135deg,#4338ca,#818cf8); }
.nav-icon-dj { background: linear-gradient(135deg,#111827,#64748b); }
.nav-icon-mobile { background: linear-gradient(135deg,#0e7490,#14b8a6); }
.nav-icon-reports { background: linear-gradient(135deg,#16a34a,#84cc16); }
.nav-icon-widget { background: linear-gradient(135deg,#9333ea,#22d3ee); }
.nav-icon-backup { background: linear-gradient(135deg,#6b7280,#d97706); }
.nav-icon-settings { background: linear-gradient(135deg,#334155,#0ea5e9); }
.nav-icon-logout { background: linear-gradient(135deg,#7f1d1d,#ef4444); }
.sidebar a:hover .nav-icon, .sidebar a.active .nav-icon { transform: translateY(-1px); box-shadow: inset 0 1px 0 rgba(255,255,255,.22), 0 8px 18px rgba(13,110,253,.25); }
        
        .sidebar-footer { margin-top: auto; border-top: 1px solid #2a2e35; padding: 8px 0; }
        .topbar {
            background: var(--topbar-bg); border-bottom: 1px solid var(--card-border);
            padding: 12px 24px; display: flex; align-items: center; gap: 12px;
            position: sticky; top: 0; z-index: 100;
        }
        .topbar-title { font-weight: 700; font-size: 0.95rem; color: #1a202c; }
        .panel { background: #fff; border: 1px solid var(--card-border); border-radius: 8px; }
        .metric-card { background: #fff; border: 1px solid var(--card-border); border-radius: 8px; padding: 16px; height: 100%; }
        .metric-value { font-size: 1.45rem; font-weight: 800; color: #0f172a; }
        .metric-label { color: #64748b; font-size: .78rem; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; }
        .flow-item { display: flex; gap: 12px; align-items: flex-start; padding: 11px 0; border-bottom: 1px solid #edf2f7; }
        .flow-item:last-child { border-bottom: 0; }
        .flow-time { min-width: 58px; font-weight: 800; color: #0d6efd; }
        .flow-type { display: inline-flex; align-items: center; border-radius: 999px; padding: 2px 8px; font-size: .72rem; font-weight: 700; background: #eef2ff; color: #4338ca; }
        .history-kind { border-radius: 999px; padding: 2px 8px; font-size: .72rem; font-weight: 700; }
        .history-kind.hour { background: #fef3c7; color: #92400e; }
        .history-kind.normal { background: #e0f2fe; color: #075985; }
        .quick-form { background: linear-gradient(135deg,#f8fafc,#eff6ff); border: 1px solid #bfdbfe; border-radius: 8px; padding: 16px; }
        .issue-dot { width: 9px; height: 9px; border-radius: 99px; display: inline-block; margin-right: 6px; }
        .issue-danger { background: #ef4444; }
        .issue-warning { background: #f59e0b; }
        .issue-info { background: #3b82f6; }
        .status-badge { border-radius: 999px; padding: 3px 9px; font-size: .72rem; font-weight: 800; }
        .status-play { background: #dcfce7; color: #166534; }
        .status-priority { background: #fef3c7; color: #92400e; }
        .status-skip { background: #fee2e2; color: #991b1b; }
        .mini-table { font-size: .84rem; }
        .mini-table td, .mini-table th { padding: .6rem .75rem; }
        .tool-panel { background: #fff; border: 1px solid var(--card-border); border-radius: 8px; padding: 16px; height: 100%; }
        .command-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; background: #101827; color: #e5edf8; border-radius: 8px; padding: 14px 16px; border: 1px solid #1f2937; box-shadow: 0 10px 26px rgba(15, 23, 42, .18); }
        .command-item { display: inline-flex; align-items: center; gap: 8px; padding: 7px 10px; background: rgba(255,255,255,.07); border: 1px solid rgba(255,255,255,.09); border-radius: 999px; font-size: .84rem; }
        .command-dot { width: 8px; height: 8px; border-radius: 99px; background: #22c55e; box-shadow: 0 0 0 4px rgba(34,197,94,.14); }
        .command-dot.warn { background: #f59e0b; box-shadow: 0 0 0 4px rgba(245,158,11,.16); }
        .calendar-tabs { gap: 8px; border-bottom: 0; }
        .calendar-tabs .nav-link { border: 1px solid #dbe4ef; border-radius: 8px; color: #475569; font-weight: 800; background: #fff; padding: 10px 14px; }
        .calendar-tabs .nav-link.active { color: #0d6efd; border-color: #93c5fd; background: #eff6ff; }
        .timeline { position: relative; padding-left: 18px; }
        .timeline::before { content: ""; position: absolute; left: 66px; top: 8px; bottom: 8px; width: 2px; background: #dbeafe; }
        .timeline-item { position: relative; display: grid; grid-template-columns: 62px 1fr; gap: 18px; padding: 10px 0; }
        .timeline-time { color: #0d6efd; font-weight: 900; font-size: .98rem; }
        .timeline-card { position: relative; background: #fff; border: 1px solid #e2e8f0; border-radius: 8px; padding: 12px 14px; }
        .timeline-card::before { content: ""; position: absolute; left: -24px; top: 16px; width: 11px; height: 11px; border-radius: 99px; background: #0d6efd; border: 3px solid #eff6ff; }
        .decision-card { border: 1px solid #e2e8f0; border-radius: 8px; padding: 12px; background: #fff; height: 100%; }
        .decision-card.play { border-left: 4px solid #22c55e; }
        .decision-card.priority { border-left: 4px solid #f59e0b; }
        .decision-card.skip { border-left: 4px solid #ef4444; opacity: .82; }
        @media (max-width: 767px) {
            .sidebar { min-height: auto; height: auto; position: static; flex-direction: row; overflow-x: auto; overflow-y: hidden; white-space: nowrap; }
            .brand { border-bottom: none; border-right: 1px solid #2a2e35; padding: 12px 14px; }
            .nav-section-label { display: none; }
            .sidebar a { padding: 12px 14px; border-left: none; border-bottom: 3px solid transparent; }
            .sidebar a.active { border-left-color: transparent; border-bottom-color: var(--accent); }
            .sidebar-footer { margin-top: 0; border-top: none; }
            .topbar { padding: 10px 16px; }
            .timeline { padding-left: 0; }
            .timeline::before { left: 54px; }
            .timeline-item { grid-template-columns: 50px 1fr; gap: 14px; }
        }
    
        /* Mobile shell fix: keep the sidebar from squeezing content on phones. */
        @media (max-width: 767px) {
            html, body { width: 100%; max-width: 100%; overflow-x: hidden; }
            .container-fluid > .row.g-0 { display: block; width: 100%; margin: 0; }
            aside.sidebar,
            .sidebar {
                width: 100% !important;
                min-width: 0 !important;
                max-width: 100% !important;
                min-height: auto !important;
                height: auto !important;
                position: relative !important;
                top: auto !important;
                flex-direction: row !important;
                align-items: stretch;
                overflow-x: auto;
                overflow-y: hidden;
                white-space: nowrap;
                -webkit-overflow-scrolling: touch;
            }
            .brand { min-width: 188px; padding: 10px 12px !important; }
            .brand img { width: 40px !important; height: 40px !important; }
            .brand-name { font-size: 1rem !important; }
            .brand-subtitle { font-size: .55rem !important; letter-spacing: .9px !important; }
            .sidebar a { flex: 0 0 auto; padding: 12px 10px !important; }
            .sidebar a .nav-icon { width: 28px; height: 28px; min-width: 28px; }
            .sidebar-footer { display: flex; flex: 0 0 auto; }
            main.col,
            main[class*="col"] {
                display: block;
                width: 100% !important;
                max-width: 100% !important;
                flex: 0 0 100% !important;
                min-width: 0 !important;
            }
            .topbar { position: sticky; top: 0; z-index: 50; padding: 10px 14px !important; }
            .topbar-title { font-size: 1rem; }
            .topbar-status { font-size: .75rem; padding: 4px 9px; }
            main > .p-4 { padding: 14px !important; }
            .card-panel { border-radius: 10px; }
            .card-panel-header { align-items: flex-start; flex-direction: column; padding: 14px !important; }
            .card-panel-header > .d-flex,
            .card-panel-header .d-flex { flex-wrap: wrap; }
            .card-panel-body { padding: 14px !important; }
            .broadcast-controls { display: grid !important; grid-template-columns: 1fr; gap: 10px; }
            .broadcast-controls .action-btn { width: 100%; min-height: 44px; }
            .stream-row,
            .stream-actions { flex-direction: column !important; align-items: stretch !important; gap: 10px !important; }
            .stream-url,
            .stream-row a { max-width: 100%; overflow-wrap: anywhere; word-break: break-word; }
            .history-item { align-items: flex-start; }
            .history-time { margin-left: 0; white-space: normal; }
            .now-playing-card { padding: 16px !important; }
            .next-track-box .next-path { white-space: normal; overflow-wrap: anywhere; }
        }
    </style>
</head>
<body>
<div class="container-fluid p-0">
    <div class="row g-0">
        <aside class="col-12 col-md-auto sidebar p-0" style="width: 220px; min-width: 220px;">
            <div class="brand">
                <img src="/static/img/akkurtcast-badge.png" alt="Akkurtcast">
                <span class="brand-mark"><span class="brand-name">Akkurtcast</span><span class="brand-subtitle">Broadcast Panel</span></span>
            </div>
            <div class="nav-section-label">Yayın</div>
            <a href="/"><span class="nav-icon nav-icon-panel"><i class="bi bi-house-door-fill"></i></span> Panel</a>
            <a href="/muzik"><span class="nav-icon nav-icon-music"><i class="bi bi-music-note-beamed"></i></span> Müzik listesi</a>
            <a href="/jingle"><span class="nav-icon nav-icon-jingle"><i class="bi bi-bell-fill"></i></span> Jingle listesi</a>
            <a href="/kutuphane"><span class="nav-icon nav-icon-library"><i class="bi bi-folder-fill"></i></span> Kütüphane</a>
            <div class="nav-section-label">Planlama</div>
            <a class="active" href="/takvim"><span class="nav-icon nav-icon-calendar"><i class="bi bi-calendar-week-fill"></i></span> Takvim</a>
            <a href="/otomasyon"><span class="nav-icon nav-icon-automation"><i class="bi bi-sliders"></i></span> Otomasyon</a>
            <a href="/haber-bulteni"><span class="nav-icon nav-icon-news"><i class="bi bi-newspaper"></i></span> Haber Bülteni</a>
            <div class="nav-section-label">Yayın Araçları</div>
            <a href="/naklen-yayin"><span class="nav-icon nav-icon-broadcast"><i class="bi bi-broadcast-pin"></i></span> Naklen Yayın</a>
            <a href="/dj"><span class="nav-icon nav-icon-dj"><i class="bi bi-headphones"></i></span> DJ</a>
            <a href="/mobil"><span class="nav-icon nav-icon-mobile"><i class="bi bi-phone-fill"></i></span> Mobil</a>
            <div class="nav-section-label">Sistem</div>
            <a href="/raporlar"><span class="nav-icon nav-icon-reports"><i class="bi bi-bar-chart-fill"></i></span> Raporlar</a>
            <a href="/widgetler"><span class="nav-icon nav-icon-widget"><i class="bi bi-puzzle-fill"></i></span> Widget</a>
            <a href="/yedek"><span class="nav-icon nav-icon-backup"><i class="bi bi-device-hdd-fill"></i></span> Yedek</a>
            <a href="/ayarlar"><span class="nav-icon nav-icon-settings"><i class="bi bi-gear-fill"></i></span> Ayarlar</a>
            <a href="/akis"><span class="nav-icon nav-icon-calendar"><i class="bi bi-list-check"></i></span> Yayın Akışı</a>
            <div class="sidebar-footer">
                <a href="/logout"><span class="nav-icon nav-icon-logout"><i class="bi bi-box-arrow-right"></i></span> Çıkış</a>
            </div>
        </aside>
        <main class="col p-0" style="min-width: 0;">
            <div class="topbar">
                <div class="topbar-title">Yayın Takvimi</div>
            </div>
            <div class="p-4">
                <div class="command-bar mb-3">
                    <span class="command-item"><span class="command-dot {{ 'warn' if durum != 'YAYINDA' else '' }}"></span>{{ durum }}</span>
                    <span class="command-item"><i class="bi bi-broadcast"></i> Sıradaki: {% if planned_next.name %}{{ planned_next.name }} {{ planned_next.time }}{% else %}Planlı anons yok{% endif %}</span>
                    <span class="command-item"><i class="bi bi-exclamation-triangle"></i> Çakışma: {{ conflicts|length }}</span>
                    <span class="command-item"><i class="bi bi-heart-pulse"></i> Sağlık: {{ playlist_health|length }}</span>
                </div>

                <div class="row g-3 mb-3">
                    <div class="col-md-3"><div class="metric-card"><div class="metric-label">Yaklaşan Akış</div><div class="metric-value">{{ upcoming|length }}</div><div class="small text-muted">Önümüzdeki 3 saat</div></div></div>
                    <div class="col-md-3"><div class="metric-card"><div class="metric-label">Çakışma</div><div class="metric-value">{{ conflicts|length }}</div><div class="small text-muted">Karar motoru aktif</div></div></div>
                    <div class="col-md-3"><div class="metric-card"><div class="metric-label">Anons Geçmişi</div><div class="metric-value">{{ announcement_history|length }}</div><div class="small text-muted">Son kayıtlar</div></div></div>
                    <div class="col-md-3"><div class="metric-card"><div class="metric-label">Sağlık Uyarısı</div><div class="metric-value">{{ playlist_health|length }}</div><div class="small text-muted">Liste ve dosya kontrolü</div></div></div>
                </div>

                <ul class="nav nav-tabs calendar-tabs mb-3" id="calendarTabs" role="tablist">
                    <li class="nav-item" role="presentation"><button class="nav-link active" data-bs-toggle="tab" data-bs-target="#tab-flow" type="button">Akış</button></li>
                    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tab-conflicts" type="button">Çakışmalar</button></li>
                    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tab-hours" type="button">Saat Anonsları</button></li>
                    <li class="nav-item" role="presentation"><button class="nav-link" data-bs-toggle="tab" data-bs-target="#tab-care" type="button">Bakım</button></li>
                </ul>

                <div class="tab-content">
                    <div class="tab-pane fade show active" id="tab-flow">
                        <div class="row g-3 mb-3">
                            <div class="col-lg-7">
                                <section class="panel h-100">
                                    <div class="px-3 py-3 border-bottom">
                                        <div class="fw-bold">Yayın Akışı Timeline</div>
                                        <div class="small text-muted">Önümüzdeki 3 saatte yayına girecek olaylar</div>
                                    </div>
                                    <div class="p-3 timeline">
                                        {% for item in upcoming %}
                                        <div class="timeline-item">
                                            <div class="timeline-time">{{ item.time }}</div>
                                            <div class="timeline-card">
                                                <div class="fw-semibold">{{ item.name }}</div>
                                                <div class="d-flex flex-wrap gap-2 align-items-center mt-1">
                                                    <span class="flow-type">{{ item.type }}</span>
                                                    {% if item.rule %}<span class="small text-muted">{{ item.rule }}</span>{% endif %}
                                                </div>
                                            </div>
                                        </div>
                                        {% else %}
                                        <div class="text-muted small">Yakın zamanda tetiklenecek kural yok.</div>
                                        {% endfor %}
                                    </div>
                                </section>
                            </div>
                            <div class="col-lg-5">
                                <section class="panel h-100">
                                    <div class="px-3 py-3 border-bottom">
                                        <div class="fw-bold">Anons Geçmişi</div>
                                        <div class="small text-muted">Son anons ve saat anonsu kayıtları</div>
                                    </div>
                                    <div class="p-3" style="max-height: 460px; overflow:auto;">
                                        {% for item in announcement_history[:12] %}
                                        <div class="d-flex align-items-start justify-content-between gap-2 py-2 border-bottom">
                                            <div>
                                                <div class="fw-semibold small">{{ item.title or item.filename }}</div>
                                                <div class="small text-muted">{{ item.artist or '' }}</div>
                                            </div>
                                            <div class="text-end">
                                                <span class="history-kind {{ 'hour' if item.kind == 'Saat Anonsu' else 'normal' }}">{{ item.kind }}</span>
                                                <div class="small text-muted mt-1">{{ item.time }}</div>
                                            </div>
                                        </div>
                                        {% else %}
                                        <div class="text-muted small">Henüz anons geçmişi yok.</div>
                                        {% endfor %}
                                    </div>
                                </section>
                            </div>
                        </div>
                    </div>

                    <div class="tab-pane fade" id="tab-conflicts">
                        {% if conflicts %}
                        <section class="panel p-3 mb-3 border-warning">
                            <div class="fw-bold mb-1 text-warning"><i class="bi bi-exclamation-triangle-fill"></i> Çakışma Kararı</div>
                            <div class="small text-muted mb-3">Saat anonsu ile normal anons aynı dakikaya denk gelirse saat anonsu öncelikli olur; normal anons o dakikayı otomatik atlar.</div>
                            {% for conflict in conflicts %}
                            <div class="mb-3">
                                <div class="fw-bold mb-2">{{ conflict.time }} dakikasında {{ conflict.count }} kural</div>
                                <div class="row g-2">
                                    {% for item in conflict.entries %}
                                    <div class="col-md-6">
                                        <div class="decision-card {% if item.action == 'Atlanacak' %}skip{% elif item.action == 'Öncelikli' %}priority{% else %}play{% endif %}">
                                            <div class="d-flex justify-content-between gap-2 align-items-center">
                                                <div class="fw-semibold">{{ item.name }}</div>
                                                <span class="status-badge {% if item.action == 'Atlanacak' %}status-skip{% elif item.action == 'Öncelikli' %}status-priority{% else %}status-play{% endif %}">{{ item.action }}</span>
                                            </div>
                                            <div class="small text-muted mt-1">{{ item.rule or item.type }}</div>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                            </div>
                            {% endfor %}
                        </section>
                        {% else %}
                        <section class="panel p-4 mb-3"><div class="fw-bold">Çakışma yok</div><div class="small text-muted">Önümüzdeki 3 saatte aynı dakikaya denk gelen kural görünmüyor.</div></section>
                        {% endif %}

                        <section class="panel mb-3">
                            <div class="px-3 py-3 border-bottom">
                                <div class="fw-bold">Kural Simülatörü</div>
                                <div class="small text-muted">Önümüzdeki 24 saatte hangi kural çalacak, hangisi atlanacak?</div>
                            </div>
                            <div class="table-responsive" style="max-height: 460px; overflow:auto;">
                                <table class="table mini-table mb-0 align-middle">
                                    <thead><tr><th>Saat</th><th>Liste</th><th>Kural</th><th>Sonuç</th></tr></thead>
                                    <tbody>
                                    {% for item in rule_simulation %}
                                    <tr>
                                        <td class="fw-bold text-primary">{{ item.time }}</td>
                                        <td>{{ item.name }}{% if item.reason %}<div class="small text-muted">{{ item.reason }}</div>{% endif %}</td>
                                        <td>{{ item.rule }}</td>
                                        <td><span class="status-badge {% if item.status == 'Atlanacak' %}status-skip{% elif item.status == 'Öncelikli' %}status-priority{% else %}status-play{% endif %}">{{ item.status }}</span></td>
                                    </tr>
                                    {% else %}
                                    <tr><td colspan="4" class="text-muted">Simüle edilecek aktif zamanlı kural yok.</td></tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </section>
                    </div>

                    <div class="tab-pane fade" id="tab-hours">
                        <section class="quick-form mb-3">
                            <form method="post" action="/saat-anonslari-olustur" class="row g-3 align-items-end">
                                <div class="col-md-4">
                                    <div class="fw-bold">Hızlı Saat Anonsu Oluşturucu</div>
                                    <div class="small text-muted">Dosyalar <strong>jingle/saat</strong> klasöründe olmalı; örnek: 7.mp3, 12.mp3, 23.mp3. Dosya eksikse sağlık kontrolünde uyarı görünür.</div>
                                </div>
                                <div class="col-md-2 col-6"><label class="form-label small fw-semibold">Başlangıç saati</label><input class="form-control" type="number" name="start_hour" min="0" max="23" value="7"></div>
                                <div class="col-md-2 col-6"><label class="form-label small fw-semibold">Bitiş saati</label><input class="form-control" type="number" name="end_hour" min="0" max="23" value="23"></div>
                                <div class="col-md-2 col-6"><label class="form-label small fw-semibold">Dakika</label><input class="form-control" type="number" name="minute" min="0" max="59" value="0"></div>
                                <div class="col-md-2 col-6"><button class="btn btn-primary w-100">Saat listelerini kur</button></div>
                            </form>
                        </section>
                        <section class="tool-panel mb-3">
                            <div class="fw-bold">Toplu Saat Anonsu Yönetimi</div>
                            <div class="small text-muted mb-3">{{ hour_playlists|length }} saat anonsu listesi bulundu.</div>
                            <form method="post" action="/saat-anonslari-toplu" class="row g-2 align-items-end">
                                <div class="col-md-4"><label class="form-label small fw-semibold">İşlem</label><select class="form-select" name="bulk_action"><option value="minute">Dakikayı güncelle</option><option value="active">Hepsini aktif yap</option><option value="passive">Hepsini pasif yap</option></select></div>
                                <div class="col-md-3"><label class="form-label small fw-semibold">Dakika</label><input class="form-control" type="number" name="minute" min="0" max="59" value="0"></div>
                                <div class="col-md-3"><button class="btn btn-outline-primary w-100">Uygula</button></div>
                            </form>
                            <div class="d-flex flex-wrap gap-2 mt-3">
                                {% for liste in hour_playlists %}
                                <span class="badge text-bg-warning">{{ liste.name }}</span>
                                {% else %}
                                <span class="small text-muted">Saat anonsu listesi yok.</span>
                                {% endfor %}
                            </div>
                        </section>
                    </div>

                    <div class="tab-pane fade" id="tab-care">
                        {% if playlist_health %}
                        <section class="panel p-3 mb-3">
                            <div class="fw-bold mb-2">Playlist Sağlık Kontrolü</div>
                            <div class="row g-2">
                                {% for issue in playlist_health %}
                                <div class="col-md-6"><div class="border rounded p-2 h-100"><div class="fw-semibold small"><span class="issue-dot issue-{{ issue.level }}"></span>{{ issue.title }}</div><div class="small text-muted">{{ issue.detail }}</div></div></div>
                                {% endfor %}
                            </div>
                        </section>
                        {% endif %}
                        <div class="row g-3">
                            <div class="col-lg-6">
                                <section class="tool-panel">
                                    <div class="fw-bold">Yayın Temizlik Modu</div>
                                    <div class="small text-muted mb-3">Boş veya dosyası eksik listeleri güvenli şekilde toparla.</div>
                                    <div class="d-flex flex-wrap gap-2 mb-3">
                                        <form method="post" action="/playlist-temizlik" onsubmit="return confirm('Boş aktif listeler pasifleştirilsin mi?');"><input type="hidden" name="cleanup_action" value="deactivate_empty"><button class="btn btn-outline-warning btn-sm">Boş aktifleri pasifleştir</button></form>
                                        <form method="post" action="/playlist-temizlik" onsubmit="return confirm('Bulunamayan dosya bağları temizlensin mi?');"><input type="hidden" name="cleanup_action" value="clear_missing"><button class="btn btn-outline-danger btn-sm">Eksik dosya bağlarını temizle</button></form>
                                    </div>
                                    {% for item in cleanup_suggestions %}
                                    <div class="d-flex justify-content-between gap-2 py-1 border-bottom small"><span><strong>{{ item.name }}</strong> · {{ item.type }}</span><span class="text-muted">{{ item.action }}</span></div>
                                    {% else %}
                                    <div class="small text-muted">Temizlik gerektiren belirgin bir kayıt yok.</div>
                                    {% endfor %}
                                </section>
                            </div>
                            <div class="col-lg-6">
                                <section class="panel">
                                    <div class="px-3 py-3 border-bottom fw-bold">Takvim Kuralları</div>
                                    <div class="table-responsive">
                                        <table class="table mini-table mb-0 align-middle">
                                            <thead><tr><th>Ad</th><th>Saat</th><th>Liste</th><th class="text-end">Hareket</th></tr></thead>
                                            <tbody>
                                            {% for rule in schedules %}
                                            <tr><td>{{ rule.name }}</td><td>{{ rule.time }}</td><td>{{ rule.playlist }}</td><td class="text-end"><form method="post" action="/takvim-sil/{{ rule.id }}"><button class="btn btn-outline-danger btn-sm">Sil</button></form></td></tr>
                                            {% else %}
                                            <tr><td colspan="4" class="text-muted">Takvim kuralı yok.</td></tr>
                                            {% endfor %}
                                            </tbody>
                                        </table>
                                    </div>
                                </section>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="d-none">
                <div class="row g-3 mb-3">
                    <div class="col-md-3"><div class="metric-card"><div class="metric-label">Yaklaşan Akış</div><div class="metric-value">{{ upcoming|length }}</div><div class="small text-muted">Önümüzdeki 3 saat</div></div></div>
                    <div class="col-md-3"><div class="metric-card"><div class="metric-label">Çakışma</div><div class="metric-value">{{ conflicts|length }}</div><div class="small text-muted">Aynı dakikaya denk gelenler</div></div></div>
                    <div class="col-md-3"><div class="metric-card"><div class="metric-label">Anons Geçmişi</div><div class="metric-value">{{ announcement_history|length }}</div><div class="small text-muted">Son kayıtlar</div></div></div>
                    <div class="col-md-3"><div class="metric-card"><div class="metric-label">Sağlık Uyarısı</div><div class="metric-value">{{ playlist_health|length }}</div><div class="small text-muted">Liste ve dosya kontrolü</div></div></div>
                </div>

                {% if conflicts %}
                <section class="panel p-3 mb-3 border-warning">
                    <div class="fw-bold mb-2 text-warning"><i class="bi bi-exclamation-triangle-fill"></i> Çakışma uyarısı</div>
                    <div class="small text-muted mb-2">Saat anonsu ile normal anons aynı dakikaya denk gelirse saat anonsu öncelikli olur; normal anons o dakikayı otomatik atlar.</div>
                    <div class="row g-2">
                        {% for conflict in conflicts %}
                        <div class="col-md-6">
                            <div class="alert alert-warning mb-0 py-2">
                                <strong>{{ conflict.time }}</strong> dakikasında {{ conflict.count }} kural var:
                                {% for item in conflict.entries %}
                                    {{ item.name }} <span class="badge text-bg-light text-dark border">{{ item.action }}</span>{% if not loop.last %}, {% endif %}
                                {% endfor %}
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </section>
                {% endif %}

                <section class="quick-form mb-3">
                    <form method="post" action="/saat-anonslari-olustur" class="row g-3 align-items-end">
                        <div class="col-md-4">
                            <div class="fw-bold">Hızlı Saat Anonsu Oluşturucu</div>
                            <div class="small text-muted">
                                Saat anonslarını çalma listesi mantığında otomatik kurar.
                                Dosyalar <strong>jingle/saat</strong> klasöründe olmalı; örnek: 7.mp3, 12.mp3, 23.mp3.
                                Dosya eksikse liste yine oluşur, sağlık kontrolünde uyarı görünür.
                            </div>
                        </div>
                        <div class="col-md-2 col-6">
                            <label class="form-label small fw-semibold">Başlangıç saati</label>
                            <input class="form-control" type="number" name="start_hour" min="0" max="23" value="7">
                        </div>
                        <div class="col-md-2 col-6">
                            <label class="form-label small fw-semibold">Bitiş saati</label>
                            <input class="form-control" type="number" name="end_hour" min="0" max="23" value="23">
                        </div>
                        <div class="col-md-2 col-6">
                            <label class="form-label small fw-semibold">Dakika</label>
                            <input class="form-control" type="number" name="minute" min="0" max="59" value="0">
                        </div>
                        <div class="col-md-2 col-6">
                            <button class="btn btn-primary w-100">Saat listelerini kur</button>
                        </div>
                    </form>
                </section>

                <div class="row g-3 mb-3">
                    <div class="col-lg-7">
                        <section class="panel h-100">
                            <div class="px-3 py-3 border-bottom">
                                <div class="fw-bold">Kural Simülatörü</div>
                                <div class="small text-muted">Önümüzdeki 24 saatte hangi kural çalacak, hangisi atlanacak?</div>
                            </div>
                            <div class="table-responsive" style="max-height: 420px; overflow:auto;">
                                <table class="table mini-table mb-0 align-middle">
                                    <thead><tr><th>Saat</th><th>Liste</th><th>Kural</th><th>Sonuç</th></tr></thead>
                                    <tbody>
                                    {% for item in rule_simulation %}
                                    <tr>
                                        <td class="fw-bold text-primary">{{ item.time }}</td>
                                        <td>{{ item.name }}{% if item.reason %}<div class="small text-muted">{{ item.reason }}</div>{% endif %}</td>
                                        <td>{{ item.rule }}</td>
                                        <td>
                                            <span class="status-badge {% if item.status == 'Atlanacak' %}status-skip{% elif item.status == 'Öncelikli' %}status-priority{% else %}status-play{% endif %}">{{ item.status }}</span>
                                        </td>
                                    </tr>
                                    {% else %}
                                    <tr><td colspan="4" class="text-muted">Simüle edilecek aktif zamanlı kural yok.</td></tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </section>
                    </div>
                    <div class="col-lg-5">
                        <section class="panel h-100">
                            <div class="px-3 py-3 border-bottom">
                                <div class="fw-bold">Anons Başarı Takibi</div>
                                <div class="small text-muted">Son çalma, toplam kayıt ve otomatik atlama durumu</div>
                            </div>
                            <div class="table-responsive" style="max-height: 420px; overflow:auto;">
                                <table class="table mini-table mb-0 align-middle">
                                    <thead><tr><th>Liste</th><th>Son çaldı</th><th>Atlandı</th></tr></thead>
                                    <tbody>
                                    {% for stat in playlist_stats if stat.group in ['normal', 'saat'] %}
                                    <tr>
                                        <td>
                                            <div class="fw-semibold">{{ stat.name }}</div>
                                            <div class="small text-muted">{{ 'Aktif' if stat.active else 'Pasif' }} · {{ stat.played_count }} kayıt</div>
                                        </td>
                                        <td>{% if stat.last_played.time %}{{ stat.last_played.time }}{% else %}<span class="text-muted">Yok</span>{% endif %}</td>
                                        <td>
                                            {{ stat.skipped_count }}
                                            {% if stat.last_skipped.time %}<div class="small text-muted">{{ stat.last_skipped.time }}</div>{% endif %}
                                        </td>
                                    </tr>
                                    {% else %}
                                    <tr><td colspan="3" class="text-muted">Anons listesi yok.</td></tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        </section>
                    </div>
                </div>

                <div class="row g-3 mb-3">
                    <div class="col-lg-7">
                        <section class="panel h-100">
                            <div class="px-3 py-3 border-bottom">
                                <div class="fw-bold">Yayın Akışı Önizleme</div>
                                <div class="small text-muted">Önümüzdeki 3 saatte tetiklenecek çalma listeleri ve anonslar</div>
                            </div>
                            <div class="p-3">
                                {% for item in upcoming %}
                                <div class="flow-item">
                                    <div class="flow-time">{{ item.time }}</div>
                                    <div>
                                        <div class="fw-semibold">{{ item.name }}</div>
                                        <div class="d-flex flex-wrap gap-2 align-items-center mt-1">
                                            <span class="flow-type">{{ item.type }}</span>
                                            {% if item.rule %}<span class="small text-muted">{{ item.rule }}</span>{% endif %}
                                        </div>
                                    </div>
                                </div>
                                {% else %}
                                <div class="text-muted small">Yakın zamanda tetiklenecek kural yok.</div>
                                {% endfor %}
                            </div>
                        </section>
                    </div>
                    <div class="col-lg-5">
                        <section class="panel h-100">
                            <div class="px-3 py-3 border-bottom">
                                <div class="fw-bold">Anons Geçmişi</div>
                                <div class="small text-muted">Sadece anons ve saat anonsu kayıtları</div>
                            </div>
                            <div class="p-3" style="max-height: 420px; overflow:auto;">
                                {% for item in announcement_history %}
                                <div class="d-flex align-items-start justify-content-between gap-2 py-2 border-bottom">
                                    <div>
                                        <div class="fw-semibold small">{{ item.title or item.filename }}</div>
                                        <div class="small text-muted">{{ item.artist or '' }}</div>
                                    </div>
                                    <div class="text-end">
                                        <span class="history-kind {{ 'hour' if item.kind == 'Saat Anonsu' else 'normal' }}">{{ item.kind }}</span>
                                        <div class="small text-muted mt-1">{{ item.time }}</div>
                                    </div>
                                </div>
                                {% else %}
                                <div class="text-muted small">Henüz anons geçmişi yok.</div>
                                {% endfor %}
                            </div>
                        </section>
                    </div>
                </div>

                {% if playlist_health %}
                <section class="panel p-3 mb-3">
                    <div class="fw-bold mb-2">Playlist Sağlık Kontrolü</div>
                    <div class="row g-2">
                        {% for issue in playlist_health %}
                        <div class="col-md-6">
                            <div class="border rounded p-2 h-100">
                                <div class="fw-semibold small"><span class="issue-dot issue-{{ issue.level }}"></span>{{ issue.title }}</div>
                                <div class="small text-muted">{{ issue.detail }}</div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </section>
                {% endif %}

                <div class="row g-3 mb-3">
                    <div class="col-lg-6">
                        <section class="tool-panel">
                            <div class="fw-bold">Toplu Saat Anonsu Yönetimi</div>
                            <div class="small text-muted mb-3">{{ hour_playlists|length }} saat anonsu listesi bulundu. Hepsini tek hamlede yönetebilirsin.</div>
                            <form method="post" action="/saat-anonslari-toplu" class="row g-2 align-items-end">
                                <div class="col-md-5">
                                    <label class="form-label small fw-semibold">İşlem</label>
                                    <select class="form-select" name="bulk_action">
                                        <option value="minute">Dakikayı güncelle</option>
                                        <option value="active">Hepsini aktif yap</option>
                                        <option value="passive">Hepsini pasif yap</option>
                                    </select>
                                </div>
                                <div class="col-md-3">
                                    <label class="form-label small fw-semibold">Dakika</label>
                                    <input class="form-control" type="number" name="minute" min="0" max="59" value="0">
                                </div>
                                <div class="col-md-4">
                                    <button class="btn btn-outline-primary w-100">Uygula</button>
                                </div>
                            </form>
                            <div class="d-flex flex-wrap gap-2 mt-3">
                                {% for liste in hour_playlists[:12] %}
                                <span class="badge text-bg-warning">{{ liste.name }}</span>
                                {% endfor %}
                                {% if hour_playlists|length > 12 %}<span class="badge text-bg-light text-dark border">+{{ hour_playlists|length - 12 }}</span>{% endif %}
                            </div>
                        </section>
                    </div>
                    <div class="col-lg-6">
                        <section class="tool-panel">
                            <div class="fw-bold">Yayın Temizlik Modu</div>
                            <div class="small text-muted mb-3">Eski test, boş veya dosyası eksik listeleri temizlemek için güvenli bakım araçları.</div>
                            <div class="d-flex flex-wrap gap-2 mb-3">
                                <form method="post" action="/playlist-temizlik" onsubmit="return confirm('Boş aktif listeler pasifleştirilsin mi?');">
                                    <input type="hidden" name="cleanup_action" value="deactivate_empty">
                                    <button class="btn btn-outline-warning btn-sm">Boş aktifleri pasifleştir</button>
                                </form>
                                <form method="post" action="/playlist-temizlik" onsubmit="return confirm('Bulunamayan dosya bağları temizlensin mi?');">
                                    <input type="hidden" name="cleanup_action" value="clear_missing">
                                    <button class="btn btn-outline-danger btn-sm">Eksik dosya bağlarını temizle</button>
                                </form>
                            </div>
                            <div style="max-height: 150px; overflow:auto;">
                                {% for item in cleanup_suggestions %}
                                <div class="d-flex justify-content-between gap-2 py-1 border-bottom small">
                                    <span><strong>{{ item.name }}</strong> · {{ item.type }}</span>
                                    <span class="text-muted">{{ item.action }}</span>
                                </div>
                                {% else %}
                                <div class="small text-muted">Temizlik gerektiren belirgin bir kayıt yok.</div>
                                {% endfor %}
                            </div>
                        </section>
                    </div>
                </div>

                <section class="panel p-4 mb-3">
                    <form method="post" action="/takvim-ekle" class="row g-3 align-items-end">
                        <div class="col-md-3">
                            <label class="form-label">Kural adı</label>
                            <input class="form-control" name="name" placeholder="Sabah akışı">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Çalma listesi</label>
                            <select class="form-select" name="playlist">
                                {% for liste in listeler %}
                                <option value="{{ liste.key }}">{{ liste.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">Başlama saati</label>
                            <input class="form-control" type="time" name="time" required>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Günler</label>
                            <div class="d-flex flex-wrap gap-2">
                                {% for value, label in gunler %}
                                <label class="form-check-label small"><input class="form-check-input" type="checkbox" name="days" value="{{ value }}"> {{ label }}</label>
                                {% endfor %}
                            </div>
                        </div>
                        <div class="col-md-1">
                            <label class="form-check-label"><input class="form-check-input" type="checkbox" name="active" checked> Aktif</label>
                        </div>
                        <div class="col-12 text-end">
                            <button class="btn btn-primary">Takvime ekle</button>
                        </div>
                    </form>
                </section>
                <section class="panel">
                    <div class="px-3 py-3 border-bottom fw-bold">Kurallar</div>
                    <div class="table-responsive">
                        <table class="table mb-0 align-middle">
                            <thead><tr><th>Ad</th><th>Saat</th><th>Liste</th><th>Günler</th><th>Durum</th><th class="text-end">Hareket</th></tr></thead>
                            <tbody>
                            {% for rule in schedules %}
                                <tr>
                                    <td>{{ rule.name }}</td>
                                    <td>{{ rule.time }}</td>
                                    <td>{{ rule.playlist }}</td>
                                    <td>{% if rule.days %}{% for day in rule.days %}{{ gun_adlari[day|string] }}{% if not loop.last %}, {% endif %}{% endfor %}{% else %}Her gün{% endif %}</td>
                                    <td>{{ 'Aktif' if rule.active else 'Pasif' }}</td>
                                    <td class="text-end"><form method="post" action="/takvim-sil/{{ rule.id }}"><button class="btn btn-outline-danger btn-sm">Sil</button></form></td>
                                </tr>
                            {% else %}
                                <tr><td colspan="6" class="text-muted">Takvim kuralı yok.</td></tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </section>
                </div>
            </div>
        </main>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script src="/static/js/mobile-nav.js"></script>
</body>
</html>









