"""
SI UI Library - Dark Theme
Catppuccin Mocha-inspired dark theme for PyQt5 applications.

Usage:
    from si_ui import apply_dark_theme, SI_DARK_STYLE

    app = QApplication(sys.argv)
    apply_dark_theme(app)
    window.setStyleSheet(SI_DARK_STYLE)
"""

from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QColor, QPalette


# =============================================================================
# COLOR CONSTANTS (Catppuccin Mocha)
# =============================================================================
COLORS = {
    "base":       "#1e1e2e",
    "mantle":     "#181825",
    "crust":      "#11111b",
    "surface0":   "#313244",
    "surface1":   "#45475a",
    "surface2":   "#585b70",
    "overlay0":   "#6c7086",
    "text":       "#cdd6f4",
    "subtext0":   "#a6adc8",
    "blue":       "#89b4fa",
    "lavender":   "#b4befe",
    "green":      "#a6e3a1",
    "teal":       "#94e2d5",
    "yellow":     "#f9e2af",
    "red":        "#f38ba8",
    "alt_base":   "#18182f",
}


# =============================================================================
# DARK PALETTE
# =============================================================================
def apply_dark_theme(app: QApplication):
    """Apply the dark Fusion palette to a QApplication instance."""
    app.setStyle("Fusion")
    palette = QPalette()
    palette.setColor(QPalette.Window,          QColor(COLORS["base"]))
    palette.setColor(QPalette.WindowText,      QColor(COLORS["text"]))
    palette.setColor(QPalette.Base,            QColor(COLORS["crust"]))
    palette.setColor(QPalette.AlternateBase,   QColor(COLORS["alt_base"]))
    palette.setColor(QPalette.Text,            QColor(COLORS["text"]))
    palette.setColor(QPalette.Button,          QColor(COLORS["surface0"]))
    palette.setColor(QPalette.ButtonText,      QColor(COLORS["text"]))
    palette.setColor(QPalette.Highlight,       QColor(COLORS["blue"]))
    palette.setColor(QPalette.HighlightedText, QColor(COLORS["base"]))
    app.setPalette(palette)


# =============================================================================
# COMPACT DARK STYLESHEET
# =============================================================================
SI_DARK_STYLE = """
QMainWindow {
    background-color: #1e1e2e;
}

QWidget {
    font-family: 'Segoe UI', Arial, sans-serif;
    font-size: 10px;
    color: #cdd6f4;
}

QFrame#panel {
    background-color: #181825;
    border-radius: 8px;
    border: 1px solid #313244;
}

QFrame#resultPanel {
    background-color: #11111b;
    border-radius: 8px;
    border: 1px solid #45475a;
}

QFrame#separator {
    background-color: #313244;
    max-height: 1px;
}

QGroupBox {
    font-weight: bold;
    font-size: 9px;
    color: #a6adc8;
    border: 1px solid #313244;
    border-radius: 6px;
    margin-top: 8px;
    padding-top: 4px;
    background-color: transparent;
}

QGroupBox::title {
    subcontrol-origin: margin;
    subcontrol-position: top left;
    left: 8px;
    padding: 0 4px;
    background-color: #181825;
}

QLabel {
    color: #cdd6f4;
    font-size: 10px;
}

QLabel#titleLabel {
    font-size: 14px;
    font-weight: bold;
    color: #89b4fa;
}

QLabel#subtitleLabel {
    font-size: 8px;
    color: #6c7086;
    letter-spacing: 1px;
}

QLabel#resultValue {
    font-size: 28px;
    font-weight: 300;
    color: #a6e3a1;
}

QLabel#resultSecondary {
    font-size: 14px;
    font-weight: 300;
    color: #94e2d5;
}

QLabel#unitLabel {
    font-size: 12px;
    color: #6c7086;
}

QLabel#rangeLabel {
    font-size: 9px;
    color: #f9e2af;
    background-color: rgba(249, 226, 175, 0.1);
    padding: 2px 6px;
    border-radius: 3px;
}

QLabel#infoLabel {
    font-size: 9px;
    color: #6c7086;
}

QPushButton {
    background-color: #313244;
    border: 1px solid #45475a;
    border-radius: 4px;
    color: #cdd6f4;
    font-size: 10px;
    padding: 6px 12px;
    min-height: 12px;
}

QPushButton:hover {
    background-color: #45475a;
}

QPushButton:pressed {
    background-color: #585b70;
}

QPushButton:disabled {
    background-color: #1e1e2e;
    color: #45475a;
}

QPushButton#primaryBtn {
    background-color: #89b4fa;
    border: none;
    color: #1e1e2e;
    font-weight: 600;
    font-size: 11px;
    padding: 8px 16px;
}

QPushButton#primaryBtn:hover {
    background-color: #b4befe;
}

QPushButton#primaryBtn:disabled {
    background-color: #45475a;
    color: #6c7086;
}

QPushButton#calculateBtn {
    background-color: #89b4fa;
    border: none;
    color: #1e1e2e;
    font-weight: 600;
    font-size: 11px;
    padding: 8px 16px;
}

QPushButton#calculateBtn:hover {
    background-color: #b4befe;
}

QPushButton#calculateBtn:disabled {
    background-color: #45475a;
    color: #6c7086;
}

QPushButton#smallBtn {
    padding: 4px 8px;
    font-size: 9px;
}

QLineEdit {
    background-color: #11111b;
    border: 1px solid #313244;
    border-radius: 4px;
    color: #cdd6f4;
    font-size: 10px;
    padding: 6px 8px;
}

QLineEdit:focus {
    border-color: #89b4fa;
}

QLineEdit#largeInput {
    font-size: 16px;
    font-weight: 300;
    padding: 8px 12px;
    border-radius: 6px;
}

QLineEdit#resistanceInput {
    font-size: 16px;
    font-weight: 300;
    padding: 8px 12px;
    border-radius: 6px;
}

QTableWidget {
    background-color: #11111b;
    border: 1px solid #313244;
    border-radius: 4px;
    gridline-color: #313244;
    font-size: 9px;
}

QTableWidget::item {
    padding: 3px;
}

QTableWidget::item:selected {
    background-color: rgba(137, 180, 250, 0.2);
}

QHeaderView::section {
    background-color: #181825;
    color: #a6adc8;
    font-weight: 600;
    font-size: 8px;
    padding: 4px;
    border: none;
    border-bottom: 1px solid #313244;
}

QTextEdit {
    background-color: #11111b;
    border: 1px solid #313244;
    border-radius: 4px;
    color: #a6adc8;
    font-family: 'Consolas', monospace;
    font-size: 9px;
    padding: 4px;
}

QScrollBar:vertical {
    background: #11111b;
    width: 6px;
    border-radius: 3px;
}

QScrollBar::handle:vertical {
    background: #45475a;
    border-radius: 3px;
    min-height: 20px;
}

QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {
    height: 0;
}

QSplitter::handle {
    background-color: #313244;
}

QSplitter::handle:horizontal {
    width: 2px;
}
"""
