"""
theme.py — Flat minimal light theme (Odoo-style)
8px spacing system · No gradient · No shadow · Clean borders
"""
import os as _os

# Asset directory (resolved at import time)
_ASSET_DIR = _os.path.join(_os.path.dirname(__file__), "assets").replace("\\", "/")

# Color palette
COLORS = {
    "bg_light":           "#f5f5f5",
    "bg_white":           "#ffffff",
    "bg_sidebar":         "#ffffff",
    "bg_sidebar_hover":   "#f0f0f0",
    "bg_sidebar_active":  "#eff6ff",
    "accent":             "#2563eb",
    "accent_light":       "#93c5fd",
    "accent_hover":       "#1d4ed8",
    "success":            "#16a34a",
    "success_bg":         "#f0fdf4",
    "danger":             "#dc2626",
    "danger_bg":          "#fef2f2",
    "warning":            "#d97706",
    "warning_bg":         "#fffbeb",
    "text_primary":       "#111827",
    "text_secondary":     "#6b7280",
    "text_muted":         "#9ca3af",
    "border":             "#e5e7eb",
    "table_alt":          "#f9fafb",
}

# Spacing scale (4-base)
SP = {"XS": 4, "S": 8, "M": 16, "L": 24, "XL": 32}

MAIN_STYLESHEET = f"""
/* ===== Reset ===== */
QWidget {{
    background-color: {COLORS['bg_light']};
    color: {COLORS['text_primary']};
    font-family: "Segoe UI", "Inter", sans-serif;
    font-size: 10pt;
    outline: none;
}}

/* ===== Sidebar ===== */
#sidebar {{
    background-color: {COLORS['bg_sidebar']};
    border-right: 1px solid {COLORS['border']};
    min-width: 220px;
    max-width: 220px;
}}

#sidebar_logo {{
    background-color: {COLORS['bg_sidebar']};
    border-bottom: 1px solid {COLORS['border']};
}}

#sidebar_logo_text {{
    color: {COLORS['text_primary']};
    font-size: 13pt;
    font-weight: 700;
    background: transparent;
}}

#sidebar_sub_text {{
    color: {COLORS['accent']};
    font-size: 8pt;
    font-weight: 500;
    background: transparent;
}}

QPushButton#nav_btn {{
    background-color: transparent;
    color: {COLORS['text_secondary']};
    border: none;
    text-align: left;
    padding: 6px 12px 6px 8px;
    font-size: 10pt;
    border-radius: 8px;
    margin: 1px 6px;
}}

QPushButton#nav_btn:hover {{
    background-color: {COLORS['bg_sidebar_hover']};
    color: {COLORS['accent']};
}}

QPushButton#nav_btn[active="true"] {{
    background-color: {COLORS['bg_sidebar_active']};
    color: {COLORS['accent']};
    font-weight: 600;
    border-left: 3px solid {COLORS['accent']};
    padding-left: 5px;
}}

/* ===== App Header (global search bar) ===== */
#app_header {{
    background-color: {COLORS['bg_white']};
    border-bottom: 1px solid {COLORS['border']};
    min-height: 72px;
    max-height: 72px;
}}

#app_header_search {{
    background-color: {COLORS['bg_light']};
    border: 1px solid {COLORS['border']};
    border-radius: 6px;
    padding: 0px 12px;
    min-width: 280px;
    max-width: 400px;
    color: {COLORS['text_primary']};
}}

#app_header_search:focus {{
    border: 1px solid {COLORS['accent']};
    background-color: {COLORS['bg_white']};
}}

/* ===== Page Header ===== */
#page_header {{
    background-color: {COLORS['bg_white']};
    border-bottom: 1px solid {COLORS['border']};
}}

#page_title {{
    color: {COLORS['text_primary']};
    font-size: 16pt;
    font-weight: 700;
}}

#page_subtitle {{
    color: {COLORS['text_secondary']};
    font-size: 9pt;
}}

/* ===== Content Area ===== */
#content_area {{
    background-color: {COLORS['bg_light']};
}}

/* ===== Cards ===== */
QFrame#stat_card {{
    background-color: {COLORS['bg_white']};
    border: 1px solid {COLORS['border']};
    border-radius: 12px;
    padding: 4px;
}}

QFrame#card {{
    background-color: {COLORS['bg_white']};
    border: 1px solid {COLORS['border']};
    border-radius: 12px;
}}

/* ===== Inputs ===== */
QLineEdit, QTextEdit, QComboBox, QSpinBox, QDoubleSpinBox, QDateEdit {{
    background-color: {COLORS['bg_white']};
    border: 1px solid {COLORS['border']};
    border-radius: 6px;
    padding: 0px 10px;
    color: {COLORS['text_primary']};
    selection-background-color: {COLORS['accent_light']};
    min-height: 36px;
    max-height: 36px;
}}

QTextEdit {{
    min-height: 72px;
    max-height: 9999px;
    padding: 8px 10px;
}}

QLineEdit:focus, QComboBox:focus, QSpinBox:focus, QDoubleSpinBox:focus, QDateEdit:focus {{
    border: 1px solid {COLORS['accent']};
}}

QLineEdit:disabled, QComboBox:disabled {{
    background-color: {COLORS['bg_light']};
    color: {COLORS['text_muted']};
}}

QLineEdit[error="true"], QComboBox[error="true"], QDoubleSpinBox[error="true"] {{
    border: 1px solid {COLORS['danger']};
    background-color: {COLORS['danger_bg']};
}}

QLineEdit#input_readonly {{
    background-color: {COLORS['bg_light']};
    color: {COLORS['text_secondary']};
}}

/* ComboBox & DateEdit dropdown button — stays INSIDE border box */
QComboBox::drop-down, QDateEdit::drop-down {{
    subcontrol-origin: padding;
    subcontrol-position: top right;
    border: none;
    border-left: 1px solid {COLORS['border']};
    border-radius: 0px 6px 6px 0px;
    width: 26px;
    margin: 1px 1px 1px 0px;
    background-color: {COLORS['bg_light']};
}}

QComboBox::down-arrow, QDateEdit::down-arrow {{
    image: url({_ASSET_DIR}/arrow_down.svg);
    width: 12px;
    height: 12px;
}}

QDateEdit::drop-down:hover, QComboBox::drop-down:hover {{
    background-color: #e0e7ff;
    border-left-color: {COLORS['accent_light']};
}}

QComboBox QAbstractItemView {{
    background-color: {COLORS['bg_white']};
    border: 1px solid {COLORS['border']};
    border-radius: 8px;
    selection-background-color: {COLORS['bg_sidebar_active']};
    selection-color: {COLORS['accent']};
    outline: none;
    padding: 4px;
}}

/* ===== Buttons — Base ===== */
QPushButton {{
    background-color: {COLORS['accent']};
    color: white;
    border: none;
    border-radius: 8px;
    padding: 0px 18px;
    font-size: 10pt;
    font-weight: 600;
    min-height: 36px;
    max-height: 36px;
}}

QPushButton:hover {{
    background-color: {COLORS['accent_hover']};
}}

/* Primary */
QPushButton#btn_primary {{
    background-color: {COLORS['accent']};
    color: white;
    border: none;
    border-radius: 6px;
    padding: 0px 18px;
    font-weight: 600;
    min-width: 96px;
}}
QPushButton#btn_primary:hover {{
    background-color: {COLORS['accent_hover']};
}}
QPushButton#btn_primary:pressed {{
    background-color: #1e40af;
}}
QPushButton#btn_primary:disabled {{
    background-color: {COLORS['accent_light']};
    color: #dbeafe;
}}

/* Secondary */
QPushButton#btn_secondary {{
    background-color: {COLORS['bg_white']};
    color: {COLORS['text_primary']};
    border: 1px solid {COLORS['border']};
    border-radius: 6px;
    padding: 0px 18px;
    font-weight: 400;
    min-width: 80px;
}}
QPushButton#btn_secondary:hover {{
    border-color: {COLORS['accent']};
    color: {COLORS['accent']};
    background-color: {COLORS['bg_sidebar_active']};
}}
QPushButton#btn_secondary:disabled {{
    color: {COLORS['text_muted']};
    border-color: {COLORS['border']};
}}

/* Danger */
QPushButton#btn_danger {{
    background-color: {COLORS['danger']};
    color: white;
    border: none;
    border-radius: 6px;
    padding: 0px 18px;
    font-weight: 600;
    min-width: 80px;
}}
QPushButton#btn_danger:hover {{
    background-color: #b91c1c;
}}
QPushButton#btn_danger:pressed {{
    background-color: #991b1b;
}}

/* Ghost */
QPushButton#btn_ghost {{
    background-color: transparent;
    color: {COLORS['accent']};
    border: 1px solid {COLORS['accent_light']};
    border-radius: 6px;
    padding: 0px 12px;
    font-weight: 400;
    min-height: 30px;
    max-height: 30px;
}}
QPushButton#btn_ghost:hover {{
    background-color: {COLORS['bg_sidebar_active']};
}}

/* Icon-only */
QPushButton#btn_icon {{
    background-color: transparent;
    border: none;
    border-radius: 4px;
    font-size: 14px;
    min-height: 30px;
    max-height: 30px;
    min-width: 30px;
    max-width: 30px;
    padding: 0px;
}}
QPushButton#btn_icon:hover {{
    background-color: {COLORS['bg_sidebar_hover']};
}}

/* ===== Tables ===== */
QTableWidget, QTableView {{
    background-color: {COLORS['bg_white']};
    border: 1px solid {COLORS['border']};
    border-radius: 6px;
    gridline-color: {COLORS['border']};
    alternate-background-color: {COLORS['table_alt']};
    color: {COLORS['text_primary']};
    selection-background-color: {COLORS['bg_sidebar_active']};
    selection-color: {COLORS['accent']};
}}

QHeaderView::section {{
    background-color: {COLORS['bg_light']};
    color: {COLORS['text_secondary']};
    border: none;
    border-bottom: 1px solid {COLORS['border']};
    border-right: 1px solid {COLORS['border']};
    padding: 8px 12px;
    font-weight: 600;
    font-size: 9pt;
    text-transform: uppercase;
}}

QHeaderView::section:last {{
    border-right: none;
}}

/* ===== Tabs ===== */
QTabWidget::pane {{
    border: 1px solid {COLORS['border']};
    background-color: {COLORS['bg_white']};
    border-radius: 0px;
    top: -1px;
}}

QTabBar::tab {{
    background-color: transparent;
    color: {COLORS['text_secondary']};
    padding: 8px 20px;
    border-bottom: 2px solid transparent;
    font-size: 10pt;
}}

QTabBar::tab:selected {{
    color: {COLORS['accent']};
    border-bottom: 2px solid {COLORS['accent']};
    font-weight: 600;
}}

QTabBar::tab:hover:!selected {{
    color: {COLORS['text_primary']};
    border-bottom: 2px solid {COLORS['border']};
}}

/* ===== Group Box ===== */
QGroupBox {{
    font-weight: 600;
    font-size: 10pt;
    color: {COLORS['text_secondary']};
    border: 1px solid {COLORS['border']};
    border-radius: 6px;
    margin-top: 12px;
    padding-top: 8px;
}}

QGroupBox::title {{
    subcontrol-origin: margin;
    subcontrol-position: top left;
    padding: 0 8px;
    left: 12px;
    color: {COLORS['text_secondary']};
}}

/* ===== Scroll Bar ===== */
QScrollBar:vertical {{
    background: {COLORS['bg_light']};
    width: 8px;
    margin: 0;
    border-radius: 4px;
}}
QScrollBar::handle:vertical {{
    background: {COLORS['border']};
    min-height: 32px;
    border-radius: 4px;
}}
QScrollBar::handle:vertical:hover {{
    background: {COLORS['text_muted']};
}}
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{
    height: 0;
}}
QScrollBar:horizontal {{
    background: {COLORS['bg_light']};
    height: 8px;
    border-radius: 4px;
}}
QScrollBar::handle:horizontal {{
    background: {COLORS['border']};
    min-width: 32px;
    border-radius: 4px;
}}

/* ===== Labels ===== */
QLabel#label_muted {{
    color: {COLORS['text_secondary']};
    background-color: transparent;
}}

/* ===== Status Bar ===== */
QStatusBar {{
    background-color: {COLORS['bg_white']};
    color: {COLORS['text_secondary']};
    border-top: 1px solid {COLORS['border']};
    font-size: 8pt;
}}

/* ===== Checkbox ===== */
QCheckBox {{
    spacing: 8px;
    color: {COLORS['text_primary']};
}}
QCheckBox::indicator {{
    width: 16px;
    height: 16px;
    border: 1px solid {COLORS['border']};
    border-radius: 4px;
    background-color: {COLORS['bg_white']};
}}
QCheckBox::indicator:checked {{
    background-color: {COLORS['accent']};
    border-color: {COLORS['accent']};
}}

/* ===== Splitter ===== */
QSplitter::handle {{
    background-color: {COLORS['border']};
    width: 1px;
    height: 1px;
}}

/* ===== Dialog ===== */
QDialog {{
    background-color: {COLORS['bg_white']};
}}
"""


def apply_theme(app) -> None:
    """Áp dụng MAIN_STYLESHEET cho toàn bộ app."""
    app.setStyleSheet(MAIN_STYLESHEET)
